Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bubble-up function attributes to FSharpFunc<_,_> so they can be used more easily #17858

Open
64J0 opened this issue Oct 8, 2024 · 3 comments

Comments

@64J0
Copy link

64J0 commented Oct 8, 2024

Is your feature request related to a problem? Please describe.

Hello, hope you're good. I'm taking a look at the available approaches for reading custom attributes from a function definition and eventually found this page from StackOverflow: https://stackoverflow.com/a/65157463/10146684.

The way of doing this, according to this answer from SO, demands the knowledge of the module that the function is defined, which does not really fit my needs.

Then, I thought that it could be more practical if the attributes defined for a specific function were "bubbled-up" to the FSharpFunc<_,_> instance, so we can directly retrieve those attributes from it.

Disclaimer: I don't know the real impact of this change, but at least in theory I think it makes sense.

Describe the solution you'd like

Replicate the function attributes at the FSharpFunc<_,_> instance to make it easier to retrieve this information for functions, using something like:

type MyAttribute(value: int) =
    inherit System.Attribute()
    member this.Value = value

[<My(42)>]
let myFunction() = ()

myFunction.GetType().GetCustomAttributes(true)

Describe alternatives you've considered

Additional context

@En3Tho
Copy link
Contributor

En3Tho commented Oct 8, 2024

Techinally a function in a module is a static method in a static class while fsharpfunc is a dedicated type.

This can create friction as there ateibutes that have strict attribute usage e.g. method or class etc.

You could bubble up some attributes to a synthesized type but not all of them due to these constraints.

I don't think it's a good idea tbh.

I believe there were proposals along of methodinfoof(...) in C# to simplify scenarios like these but they never got real traction.

@T-Gro
Copy link
Member

T-Gro commented Oct 14, 2024

F#9 did have big improvements in attribute targets, which would fail with this - I guess the expectation is to annotate the function like a method, but the runtime representation of the FsharpFunc is a class.

@vzarytovskii
Copy link
Member

Yeah, we might need to think where to emit them - on type, on Invoke method, on method which has the same name as function? In all of those cases - do we "special-case" the attribute targets and widen them to be allowed on more targets than it (possibly) initially is?
Also, a separate discussion is what do we do on anonymous funcions? Right now attributes aren't propagated anywhere from them, and I think it was strictly rejected as suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: New
Development

No branches or pull requests

4 participants