-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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: target typing for "default" in C# #13255
Comments
I really don't like this part of the proposal. Also, I think it would be a breaking change, at least as long as C# sticks with the "methods in derived class have precedence over methods from base class" principle in overload resolution. |
Also, #35 is closely related. With that, the code would be: ImmutableArray<string> x = new(); |
Looks like #7737. I prefer void M(int value = default, object obj = default) { .. }
// compared to
void M(int value = new(), object obj = null) { .. } I'm not against #35 but, in my opinion, it is not a replacement for |
I'd agree with @alrz that #35 is a distraction here and that this proposal is a useful one (where as I don't believe #35 is useful). What use is Whereas, for example, being able to rewrite: TR F<T, TR>(T p) => (some expression of p) ? default(TR) : (some val of TR); as TR F<T, TR>(T p) => (some expression of p) ? default : (some val of TR); would be a nice noise-reducing addition to the language. |
@DavidArno Not to distract, but I think #35 comes in handy in field initialization where we don't have |
@gafter |
@AlgorithmsAreCool IIRC this was already prototyped once, but some internal CLR subsystems relied on the assumption that all |
@AlgorithmsAreCool @orthoxerox The issue for the revert of that prototype is #1029. |
I agree with @svick that |
Oh my. That is incredible news. The inability to declare a no-args constructor in struct types in my single biggest complaint about them, eg with the need to include an unused parameter (and then suppress CA1801) when declaring a private constructor that should have no parameters). |
I like the proposal. Make sure that "default" does not have default type. :-) VB Nothing defaults to Object. There are reasons for that, but in a case of C# it would only introduce unnecessary complexities. |
It would indeed compliment the default struct constructors. Default struct constructors and instance field initializers were at production stage in C#6.0 but ran into bugs in various runtimes. Most prominently the issue in Activator.CreateInstance which resulted in a broken generic |
The
default
keyword currently requires an explicit type.For instance, one has to write
default(ImmutableArray<string>)
.But the type could be omitted in most cases, resulting in simpler code:
default
.You would be allowed to write:
M(default);
,x = default;
, wherex
is known to have typeImmutableArray<string>
.Note this feature is already available in VB, as you can use the
Nothing
keyword for the default value (both for class and struct types).If we wanted to follow the VB approach, the proposal could extend to allowing
null
as default value for value types as well, such asImmutableArray<string> x = null;
.FYI @dotnet/roslyn-compiler
Update: As @svick pointed out, proposal #35 to allow type inference in constructions (
new ()
), but @gafter clarified this would have different meaning.Update: We should allow:
const int x = default;
(using target-typeddefault
for constant)object x = default;
(using target-typeddefault
for reference type)The text was updated successfully, but these errors were encountered: