Relax nameof() #1927
-
TLDR:
|
Beta Was this translation helpful? Give feedback.
Replies: 13 comments
-
I don't like it, as I would expect EDIT: retracted, now I've bothered reading the documentation for nameof.🤣 |
Beta Was this translation helpful? Give feedback.
-
We can't change I believe we have a few other proposals that suggest returning the "qualified" or "fully qualified" name |
Beta Was this translation helpful? Give feedback.
-
Actually I was wanting nameof(Children.Count) to just return "Count". In
other places I can do that, but not in attributes.
Tony Valenti
Schedule a meeting with me <https://Fasterlaw.com/schedule-meeting>
…On Wed, Oct 10, 2018 at 12:24 PM Tanner Gooding ***@***.***> wrote:
We can't change nameof(Children.Count) to return "Children.Count" as that
would be a breaking change for a good bit of existing code.
I believe we have a few other proposals that suggest returning the
"qualified" or "fully qualified" name
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://github.com/dotnet/csharplang/issues/1927#issuecomment-428658110>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AM-qVkspsht2h9XpU7j3PfLK_ubBMGL7ks5uji0ygaJpZM4XVlq0>
.
|
Beta Was this translation helpful? Give feedback.
-
@tannergooding EDIT: retracted, now I've bothered reading the documentation for nameof.🤣 |
Beta Was this translation helpful? Give feedback.
-
@tannergooding How would it be breaking if this is not currently valid syntax? |
Beta Was this translation helpful? Give feedback.
-
I was replying to @YairHalberstadt's comment and should have clarified as such 😄 |
Beta Was this translation helpful? Give feedback.
-
using System;
public class C {
public static void Main() {
Console.WriteLine(nameof(Children.Count)); // prints Count
}
public class Children {
public static int Count { get; set; }
}
} |
Beta Was this translation helpful? Give feedback.
-
The proposed syntax would be breaking because you'd end up with ambiguous code. The "new" syntax suggested here can already compile, even within an attribute: public class ViewModel {
public ObservableCollection<string> Children { get; private set; }
= new ObservableCollection<string>();
[DependsOn(nameof(Children), nameof(Children.Count))]
public bool HasChildren {
get {
var C = nameof(Children.Count);
return Children.Count > 0;
}
}
}
// Elsewhere in the project, but within a namespace already used
public class Children
{
public int Count { get; }
} |
Beta Was this translation helpful? Give feedback.
-
My general view is that there's no point adding a second way of doing something, until you are sure that the syntax won't clash with something else. At the moment the syntax is something like:
The request is to add a new syntax
I feel like that is a very different syntax which I would expect to have a different meaning. As such I would a) reserve it, in case we want to use it later. b) not use it for this purpose either way, to increase consistency (only one way of doing things) and following principle of least surprise. Then again, I wouldn't particularly care if it was introduced. Just voicing some points. EDIT: retracted, now I've bothered reading the documentation for nameof.🤣 |
Beta Was this translation helpful? Give feedback.
-
@HaloFour - |
Beta Was this translation helpful? Give feedback.
-
@YairHalberstadt There can already be an unlimited number of member names and I think everyone is familiar with the behavior of only taking the specified member's name. using System;
public class C
{
public void M()
{
// Writes "Member3", not "this.Member1.Member2.Member3" or "Member1.Member2.Member3"
Console.WriteLine(nameof(this.Member1.Member2.Member3));
}
public C Member1 { get; }
public C Member2 { get; }
public C Member3 { get; }
} |
Beta Was this translation helpful? Give feedback.
-
@jnm2 |
Beta Was this translation helpful? Give feedback.
-
This is actually a compiler error and should be legal. See dotnet/roslyn#40229. |
Beta Was this translation helpful? Give feedback.
This is actually a compiler error and should be legal. See dotnet/roslyn#40229.