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

Proposal: NameOf to have optional parameters. #3759

Closed
AdamSpeight2008 opened this issue Jun 30, 2015 · 21 comments
Closed

Proposal: NameOf to have optional parameters. #3759

AdamSpeight2008 opened this issue Jun 30, 2015 · 21 comments
Labels
Area-Language Design Resolution-Won't Fix A real bug, but Triage feels that the issue is not impactful enough to spend time on.

Comments

@AdamSpeight2008
Copy link
Contributor

Proposal: NameOf to have optional parameters.

Currently.
nameof(Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider) = "CodeFixProvider"
(See #3708)

To fix this I propose that we should extend NameOf with optional parameters. , NameOfOptions:= )

NameOfOption Enum

Enum NameOfOption As Integer

  [default] = 0
  fullname  = 1

End Enum

Api signature

NameOf(  obj As Object, Optional NameOfOptions As NameOfOption = Nothing ) As String

Example

nameof(Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider, NameOfOptions:= NameOfOption.FullName )

This would then provide the fully-qualified name.

It provides a compile-time solution, to what (I believe) requires the use of reflection, or explicitly stating the full name as a string literal. Hence introducing a potential bug if it changes namespace or name.

Does not break existing usage because the parameter is optional.

@svick
Copy link
Contributor

svick commented Jul 1, 2015

In what situations would this be useful?

@AdamSpeight2008
Copy link
Contributor Author

@svick See #3708.

@leppie
Copy link
Contributor

leppie commented Jul 1, 2015

nameof is a compile time construct, it has no API.

A better solution would be to use pragmas to control the output/options.

@paulomorgado
Copy link

That has been largely discussed on Codeplex.

It was even discussed FullNameOf.

@hishamco
Copy link
Member

hishamco commented Jul 1, 2015

@svick this may useful in `Exception`` when the argument is property path, have a look to this aspnet/WebSockets@208fa4a#diff-2f20d3473af8bc60eee727dd88bbb804R595

@Eilon
Copy link
Member

Eilon commented Jul 1, 2015

The parameters to argument exceptions are never "property paths" - they are parameter names. Passing in foo.Bar.Baz to an ArgumentException is invalid.

@whoisj
Copy link

whoisj commented Jul 1, 2015

What is wrong with $"The name is {nameof(value)} and its type is {typeof(value)}, and the type's full name is {typeof(value).FullName}, and {nameof(value)} has a property of {nameof(value.Property)}."?

@hishamco
Copy link
Member

hishamco commented Jul 1, 2015

@Eilon that's exactly what I mean foo.bar.baz 😄

@Eilon
Copy link
Member

Eilon commented Jul 1, 2015

@whoisj that's exactly what I recommend people to use.

@whoisj
Copy link

whoisj commented Jul 1, 2015

@Eilon I know. My comment was towards the original post. 😏

@hishamco
Copy link
Member

hishamco commented Jul 1, 2015

I don't think so that will applicable for value.Length where value is String

@whoisj
Copy link

whoisj commented Jul 1, 2015

I don't think so that will applicable for value.Length where value is String

What won't be?

Given void 'MyFunc(string value)'

  • nameof(value) == "value"
  • typeof(value) == System.String
  • nameof(value.Length) == "Length"
  • typeof(value.Length) == System.Int32

@hishamco
Copy link
Member

hishamco commented Jul 1, 2015

@whoisj our intend to display something like foo.Bar.Baz using nameof which is an implementation limitation :(

@whoisj
Copy link

whoisj commented Jul 1, 2015

@hishamco then I go back to my original statement: what is wrong with $"{nameof(foo)}.{nameof(foo.Bar)}.{nameof(foo.Bar.Baz)}" ?

@hishamco
Copy link
Member

hishamco commented Jul 1, 2015

Looks good trick 😄

@svick
Copy link
Contributor

svick commented Jul 2, 2015

@AdamSpeight2008 So this is just a performance optimization of typeof(CodeFixProvider).FullName? That doesn't sounds like it would be worth inventing new syntax to me.

@whoisj
Copy link

whoisj commented Jul 2, 2015

@AdamSpeight2008 So this is just a performance optimization of typeof(CodeFixProvider).FullName? That doesn't sounds like it would be worth inventing new syntax to me.

I said the same thing, they're not looking for Type.FullName, they're looking for the texted used in the parameter validation. Example:

if (foo.Bar.Baz == null) throw new NullArgumentException(nameof(foo.Bar.Baz));

They want to see "foo.Bar.Baz" returned by the nameof macro, but they're only getting "Baz".

@leppie
Copy link
Contributor

leppie commented Jul 3, 2015

I agree with @svick . Also some syntax sugar for typeof(Foo).Fullname cannot be resolved reliably at compile-time, so there is no optimization to be had.

I pondered during last week on Twitter if something like the following would not be useful (also mentioned by @whoisj):

var i = 0;
typeof(i) j = 1; // here typeof(i) is an alias for the same type as i
Foo<typeof(i)>(); // same here
Console.Write(typeof(typeof(i))); // prints System.Int32

The tiny benefit here is you only need to change the type of i (say to byte or long) and the rest of the code 'refactors' itself. Still not sure if such a benefit is worth it. Would be another story if C# had macros or templates.

@AdamSpeight2008
Copy link
Contributor Author

@leppie

cannot be resolved reliably at compile-time?

How? Doesn't the compiler currently do type-checking at compile-time, which suggests to me the the fully qualified name is also available..

@leppie
Copy link
Contributor

leppie commented Jul 3, 2015

@AdamSpeight2008 typeof only loads the type token as determined at compile time, fullname would be constant string. Now imagine some AOP process changing something (namespace, type), etc. If something bad happened, peverify will say the type token is now invalid, but with a string, all bets are off. Perhaps this will never happen, but who knows?

@gafter
Copy link
Member

gafter commented Sep 6, 2015

Regardless of whether we would ever provide support for getting a fully qualified name (seems unlikely), we would not do it in the way requested here.

@gafter gafter closed this as completed Sep 6, 2015
@gafter gafter added the Resolution-Won't Fix A real bug, but Triage feels that the issue is not impactful enough to spend time on. label Sep 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Language Design Resolution-Won't Fix A real bug, but Triage feels that the issue is not impactful enough to spend time on.
Projects
None yet
Development

No branches or pull requests

8 participants