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]: Allow nameof(method parameter) outside the method. #4413

Open
1 of 4 tasks
Youssef1313 opened this issue Feb 10, 2021 · 9 comments
Open
1 of 4 tasks

[Proposal]: Allow nameof(method parameter) outside the method. #4413

Youssef1313 opened this issue Feb 10, 2021 · 9 comments

Comments

@Youssef1313
Copy link
Member

Youssef1313 commented Feb 10, 2021

Allow nameof(method_parameter_name)

  • Proposed
  • Prototype: Not Started
  • Implementation: Not Started
  • Specification: Not Started

Summary

Currently, there is no way to use a parameter name outside the method without hardcoding it.

Motivation

Extending nameof capabilities and allowing it to be used in more contexts. Similar to: #4384, and extends #373 (not a dupe of it)

Detailed design

While #373 suggests allowing nameof for parameter names in attributes applied to the method, this proposal is a suggestion to generalize it to be allowed outside the method context. A real-world example that can benefit from this:

https://github.com/dotnet/roslyn-analyzers/blob/b76ff2f/src/Microsoft.CodeAnalysis.Analyzers/Core/MetaAnalyzers/DiagnosticDescriptorCreationAnalyzer.cs#L29-L35

public class C
{
    public void M()
    {
        var x = nameof(M.s); // Same as var x = "s";
        var x = nameof(M.y); // Compile error.
    }
    
    public void M(object s) { }
}

Drawbacks

  • A simple member access expression for parameter name could be confusing (consider another operator?)
  • How IDE should work in case of renaming?

Alternatives

Unresolved questions

Design meetings

@YairHalberstadt
Copy link
Contributor

Would you be able to access members of the parameter as well? E.g. in your example nameof(M.s.ToString)?

@Youssef1313
Copy link
Member Author

Would you be able to access members of the parameter as well? E.g. in your example nameof(M.s.ToString)?

Probably depends on the ease of implementation. There is already a cleaner way to do that (nameof(WhateverTypeTheArgumentIs.ToString)).

@HaloFour
Copy link
Contributor

The syntax of using member access to reference a parameter of a method feels really weird here, as does the name resolution (although you could argue that the latter is already "broken" by method group resolution). It gets much weirder if you can dot into the members of the parameter type, especially if there are overloads of that method that could have parameters of the same name but of different types.

What are the use cases where you'd want to take the name of a method parameter in a separate method body?

@Youssef1313
Copy link
Member Author

The syntax of using member access to reference a parameter of a method feels really weird here, as does the name resolution (although you could argue that the latter is already "broken" by method group resolution). It gets much weirder if you can dot into the members of the parameter type, especially if there are overloads of that method that could have parameters of the same name but of different types.

What are the use cases where you'd want to take the name of a method parameter in a separate method body?

The use case that come to mind is mostly for analyzers, such as the example provided.


It gets much weirder if you can dot into the members of the parameter type, especially if there are overloads of that method that could have parameters of the same name but of different types.

That's correct. In that case I think accessing into the members of the parameter should be completely disallowed.


The syntax of using member access to reference a parameter of a method feels really weird here

Could we consider another token other than the DotToken?

@ashmind
Copy link

ashmind commented Mar 5, 2021

What are the use cases where you'd want to take the name of a method parameter in a separate method body?

One use case is to register specific parameter value during dependency injection registration.
It's a bit of an edge case, but I still run into it periodically. Something like this (artificial):

builder.RegisterType<Processor>()
       .As<IProcessor>()
       .WithParameter("timeout", config.Timeout);

Note that it requires nameof access to constructor parameters though.

@TahirAhmadov
Copy link

Will this support parameters of methods in other types?

const string s = nameof(My.Namespace.Class.Method.parameter);

@333fred
Copy link
Member

333fred commented Mar 10, 2022

Currently, there is no proposal for adding such semantics.

@Jawvig
Copy link

Jawvig commented Mar 18, 2022

Another example of usage would be attributes. For instance, in the Open API & Azure Functions attributes OpenApiParameterAttribute and HttpTriggerAttribute respectively. For example:

/*...*/
[OpenApiParameter(name: nameof(FuncClass.SomeFunction.someParam), In = ParameterLocation.Path /*, ... */)]
/*...*/
public async Task<IAsyncResult> SomeFunction(
    [HttpTrigger(/*...*/ Route=$"abc/{{{nameof(FuncClass.SomeFunction.someParam}}}/abc")] HttpRequest request,
    string  someParam)
{
   /* ... */
}

Even better would be if some of the context could be inferred so that the class or method name don't need to be applied based on the target of the attribute.

@Jawvig
Copy link

Jawvig commented Mar 18, 2022

Never mind, I should have read the proposal in more detail as the use in attributes is in #373

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

7 participants