[RFC FS-1003 Discussion] nameof Operator #48
Replies: 76 comments
-
I added some comments to the proposed PR here: dotnet/fsharp#908 (comment) I'm concerned about the implementation strategy and think it should probably be done earlier in the type checker. Some clarifications to the spec are needed
|
Beta Was this translation helpful? Give feedback.
-
Since nameof is just the name, ambiguous overloaded methods still all have the same name, so it should work. |
Beta Was this translation helpful? Give feedback.
-
@thinkbeforecoding I believe |
Beta Was this translation helpful? Give feedback.
-
@dsyme where should I try to hook it in? |
Beta Was this translation helpful? Give feedback.
-
I think it's captured by this comment:
So there would be a special case in the type checker |
Beta Was this translation helpful? Give feedback.
-
Yes I got that. I was wondering if we already have a place where we pattern
|
Beta Was this translation helpful? Give feedback.
-
Perhaps the closest is in There are also similarities with the processing of the |
Beta Was this translation helpful? Give feedback.
-
I don't see any mention of What is the expected behaviour in interaction with that attribute? |
Beta Was this translation helpful? Give feedback.
-
Interesting question. |
Beta Was this translation helpful? Give feedback.
-
Main purpose of nameof is probably some logging purposes, and it would be useful on some async scenarios when the stacktrace won't tell you all and you can't reraise. I would also vote the name in F# source code. |
Beta Was this translation helpful? Give feedback.
-
I want nameof mostly for some amount of type safety in my reflection code. When the name of a type is changed I want to be able to get an error (or gui rename-update) for that change. I think ideally we would be able to just copy what C# nameof does. |
Beta Was this translation helpful? Give feedback.
-
New implementation at dotnet/fsharp#2290 |
Beta Was this translation helpful? Give feedback.
-
@Rickasaurus comment makes me think we could also have |
Beta Was this translation helpful? Give feedback.
-
I had some trouble understanding the whole requirements list on RFC FS-1003, would it be ok if I pulled it and add one or two examples to each bullet point for clarity? I also noticed two other things that are either missing or deliberately not included:
Also, I was wondering about the first couple of bullet points. It seems to me this should work as a keyword (like |
Beta Was this translation helpful? Give feedback.
-
C# got some nice "statically" typed patterns with this. Hope F# would get that soon. |
Beta Was this translation helpful? Give feedback.
-
That's a pity, I kinda liked getting I've two follow up questions on the new design decisions:
In the design it's said that the resolution is 100% compile time, leading to a literal with zero overhead, so I'd assume that whatever is in the expression gets erased. |
Beta Was this translation helpful? Give feedback.
-
I think that |
Beta Was this translation helpful? Give feedback.
-
Also, since
|
Beta Was this translation helpful? Give feedback.
-
@jwosty I think we have a PR that would enable that: dotnet/fsharp#8754 @dsyme What do you think of that? |
Beta Was this translation helpful? Give feedback.
-
The name occuring in the source
The code inside the |
Beta Was this translation helpful? Give feedback.
-
The name occuring in the source
The code inside the |
Beta Was this translation helpful? Give feedback.
-
Thanks @dsyme, that clears things up. @jwosty, you and a compelling point, maybe we should already start I om a lang suggestion for |
Beta Was this translation helpful? Give feedback.
-
So it turns out that
is way harder than I had considered. OK, so at the moment F# doesn't support literal definitions in recurisve scopes that reference other literals in the recursive scope recursively or not "in order left to right". For example let rec [<Literal>] x = 1
and y = x + 1 is allowed but let rec [<Literal>] x = y+1
and y = 1 is not. This also applies to module rec SomeCode =
let [<Literal>] x = 1
let y = x + 1 The reason for this is that supporting this requires adding an additional phase to the processing of recursive definitions where literals are computed and commited into the symbols. It also requires determining cycles in the graph of literal definitions. We don't do either of these thigns at the moment. This means that let rec [<Literal>] x = 1
and y = nameof(x) is allowed but let rec [<Literal>] x = nameof(x) is not. I think we should consider this an orthogonal limitations since, as I show above, it already applies to other literal definitions. There is not much likely use for |
Beta Was this translation helpful? Give feedback.
-
I am okay with not supporting self defined constants, as it is not too difficult to work around, but would like to add that I do often use this pattern in C#. const string MagicText = nameof(MagicText); Works well for cases where a hard coded literal text is justified, the literal text is well self documenting, and it is being used in multiple locations. I've often used this pattern for the specifying the keys of an ASP.NET MVC TempData dictionary. Of course, if it is not simple enough to justify the additional feature, it is simple enough to write |
Beta Was this translation helpful? Give feedback.
-
Well you can just do let rec MagicString = nameof(MagicString) or module rec MagicStrings =
let Magic = nameof(Magic) You just don't get to define it as We can definitely consider lifting the restriction, but it should just be dealt with orthogonally I think. |
Beta Was this translation helpful? Give feedback.
-
The use for something like Also verifying from earlier in the thread that this works: type X() as this =
inherit Dictionary<string, string>()
member val XValue = this.TryGetValue(nameof this.XValue) |> function true, x -> ValueSome x | false, _ -> ValueNone
member val YValue = this.TryGetValue(nameof this.YValue) |> function true, y -> ValueSome y | false, _ -> ValueNone
member val ZValue = this.TryGetValue(nameof this.ZValue) |> function true, z -> ValueSome z | false, _ -> ValueNone |
Beta Was this translation helpful? Give feedback.
-
Should I file an issue on dotnet/fsharp for the general problem with literals? Or is that a language suggestion? |
Beta Was this translation helpful? Give feedback.
-
I guess it's a language suggestion. It's pre-approved however. |
Beta Was this translation helpful? Give feedback.
-
Note that the topic overlaps with various suggestions for extending the range of guaranteed compile-time evaluation of expressions in F#. .NET literals are frustrating because they are so limited - and languages don't really have the option of augmenting what's allowed. |
Beta Was this translation helpful? Give feedback.
-
Added fsharp/fslang-suggestions#889 - some of the example code in this thread actually requires another language suggestion, making your statement about overlap with other feature areas very true. |
Beta Was this translation helpful? Give feedback.
-
This issue is used to track discussions of F# RFC FS-1003 - "nameof Operator". Please discuss in thread below (if necessary)
Beta Was this translation helpful? Give feedback.
All reactions