fsharp / fslang-suggestions

The place to make suggestions, discuss and vote on F# language and core library features
345 stars 21 forks source link

Enforce `AttributeTargets` properly. #1375

Open edgarfgp opened 2 months ago

edgarfgp commented 2 months ago

I propose we Enforce the correct use of AttributeTargets in the language and write a specification to document it.

The existing way of approaching this problem in F# is

In most cases just use interchangeably custom attributes with AttributeTargets and the compiler will not raise any errors.

open System

[<AttributeUsage(AttributeTargets.Method)>]
type MethodOnlyAttribute() = 
  inherit System.Attribute()

[<MethodOnly>]
let someFunction () = "abc"

[<MethodOnly>] // Should error  with: This attribute is not valid for use on this language element
let someValue =  "abc"

[<AttributeUsage(AttributeTargets.Field)>]
type FieldOnlyAttribute() = 
  inherit System.Attribute()

[<FieldOnly>] // Should error  with: This attribute is not valid for use on this language element
let someFunction () = "abc"

[<FieldOnly>]
let someValue =  "abc"

Note: The above snippet is just one of the many issues with AttributeTargets.

Pros and Cons

The advantages of making this adjustment to F# are

The disadvantages of making this adjustment to F# are

Extra information

**Estimated cost (XS, S, M, L, XL, XXL): M

Related suggestions: (put links to related suggestions here)

Affidavit (please submit!)

Please tick these items by placing a cross in the box:

Please tick all that apply:

For Readers

If you would like to see this issue implemented, please click the :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.

edgarfgp commented 2 months ago

I have already done most of the job for this and now I would like to create a RFC for this :). cc @vzarytovskii