For any struct type with implicit operator cast from a primitive type, allow it's optional-method-parameter to be set to a compiler-constant of the primitive type. #8317
-
For example, System.Index has an implicit operator with System.Int32: Now, take a look at the following MyMethodA():
A Compiler Error CS1750 is thrown for the babove method saying: A value of type cannot be used as a default parameter because there are no standard conversions to type. Now rewriting the above method for System.Decimal, no error is shown:
Therefore, the proposal here is that the implicit operator above could serve as the standard conversion. The implementation detail is simply analog to System.Decimal. The difference between this proposal and other simililar proposals on GitHub is that in this case, it's simply about assigning the optional-method-parameter to a compiler-comstant. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
I remember seeing a proposal recently for allowing more things as constants; allowing all readonly structs to be Given that, you could theoretically at least do something like this; const Index ConstantIndex = 1;
public void Method(Index index = ConstantIndex) {} |
Beta Was this translation helpful? Give feedback.
-
Not any apt comparison; public const decimal MyDecimal = 1.234m; As for this proposal, I'd much rather see the "const anything" proposal make headway. Also, what you desire is already possible with overloading: public void DoSomething(int index = 1) =>
DoSomething((Index)index);
public void DoSomething(Index index)
{
// ...
} This allows someone to leave the argument off, or to pass in an explicit |
Beta Was this translation helpful? Give feedback.
-
This is a further example I stumbled upon today. So, you can actually do a switch for ReadOnlySpan (a non-compiler constant) over constant strings. Very good one!
|
Beta Was this translation helpful? Give feedback.
-
I stumbled upon a workaround with attribute DefaultParameterValue, see below.
|
Beta Was this translation helpful? Give feedback.
I stumbled upon a workaround with attribute DefaultParameterValue, see below.
Using Visual Studio, the default value of the optional parameter is even visible both within and outside the assembly ... very good!