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

Support using nameof to generate literal strings (specifically in Attribute parameter lists). #1348

Open
5 of 6 tasks
baronfel opened this issue Jan 15, 2024 · 3 comments
Open
5 of 6 tasks

Comments

@baronfel
Copy link
Contributor

I propose we allow the nameof function to work at compile time, specifically in attribute parameter expressions like the following:

/// Represents information about programming constructs like variables, classes,
/// interfaces etc.
type SymbolInformation =
  { 
    ....
    /// Tags for this symbol.
    Tags: SymbolTag [] option

    /// Indicates if this symbol is deprecated.
    /// @deprecated Use tags instead
    [<Obsolete("Use "+ nameof(Unchecked.defaultof<SymbolInformation>.Tags) + " instead")>]
    Deprecated: bool option
    ....
}

The existing way of approaching this problem in F# is to hard code member names in the strings, leaving them prone to drift out of date.

Pros and Cons

The advantages of making this adjustment to F# are more uniform behavior of the nameof helper.

The disadvantages of making this adjustment to F# are expansion of the set of data that is computable at compile time, user confusion around when certain functions can be used.

Extra information

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

Related suggestions: #1347

Affidavit (please submit!)

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

  • This is not a question (e.g. like one you might ask on StackOverflow) and I have searched StackOverflow for discussions of this issue
  • This is a language change and not purely a tooling change (e.g. compiler bug, editor support, warning/error messages, new warning, non-breaking optimisation) belonging to the compiler and tooling repository
  • This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate

Please tick all that apply:

  • This is not a breaking change to the F# language design
  • I or my company would be willing to help implement and/or test this

For Readers

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

@Tarmil
Copy link

Tarmil commented Jan 15, 2024

Interestingly, the following works:

    [<Obsolete("Use " + nameof SymbolInformation + " instead")>]

So my impression is that this is actually a bug when handling more complex content in nameof in attributes.

@baronfel
Copy link
Contributor Author

Interesting catch - I was specifically trying to mimic some LSP Spec documentation that referred to another member, so maybe that means this is a much smaller request - extending the existing nameof replacement logic to recognize the Unchecked.defaultof<T>.member pattern?

@brianrourkeboll
Copy link

brianrourkeboll commented Jan 15, 2024

/// Represents information about programming constructs like variables, classes,
/// interfaces etc.
type SymbolInformation =
  { 
    ....
    /// Tags for this symbol.
    Tags: SymbolTag [] option

    /// Indicates if this symbol is deprecated.
    /// @deprecated Use tags instead
    [<Obsolete("Use "+ nameof(Unchecked.defaultof<SymbolInformation>.Tags) + " instead")>]
    Deprecated: bool option
    ....
}

I think it's actually just that the name resolution rules don't allow referring to the field name Tags in that context (in an attribute on another field in the same record type).

The rules for regular class members (as opposed to record fields) are a little bit more flexible, because members are already allowed to refer to each other or themselves in their implementations, and the following examples compile:

type SymbolInformation =
  { 
    /// Tags for this symbol.
    Tags: SymbolTag [] option
  }

  /// Indicates if this symbol is deprecated.
  /// @deprecated Use tags instead
  [<Obsolete("Use "+ nameof(Unchecked.defaultof<SymbolInformation>.Tags) + " instead")>]
  member _.Deprecated = Some true
[<AbstractClass>]
type SymbolInformation () =
    /// Tags for this symbol.
    abstract Tags: SymbolTag [] option

    /// Indicates if this symbol is deprecated.
    /// @deprecated Use tags instead
    [<Obsolete("Use "+ nameof(Unchecked.defaultof<SymbolInformation>.Tags) + " instead")>]
    abstract Deprecated: bool option

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants