-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Prototype: target typed "default" #13602
Comments
What happens if
Yes because it save the author having to write the type again. |
|
@jcouv |
Why? Shouldn't it be equivalent to |
The compiler requires that any reference of a keyword as an identifier be prefixed with int x = 2;
int @default = 3;
int result = x + default; // oops, sets result to 2
int correct = x + @default; |
@HaloFour I don't see how is that different from the following, currently valid code: int? x = null;
int @null = 0;
int? result = x ?? null; // oops, sets result to null
int? correct = x ?? @null; |
I agree. It's not a big enough of a concern to reconsider the feature. Anyone who explicitly decides to use keywords as identifiers via |
@alrz There was no LDM discussion or decision on this, but my thinking is that |
int @default = 3;
int result = default; // oops, sets result to ?? Is that result 3 or 0. |
Indeed, I already pointed that out. But as @svick noted that's already the case with |
It think the following syntax is more in keeping with C#. F( int x?, string s?, int? n?, bool b? ) equivalent to F( int x = default(int),
string s = default(string),
int? n = default(nullable<int>),
bool b = default(bool) ) |
|
That seems unfortunate. It would be nice to have a single literal you could use for everything. |
Out of curiosity, is there any reason why we wouldn't want to make 'default' as close to 'null' as possible, except that it would also be allowed for non-reference types? |
Make it behave similarly to VB's |
@AdamSpeight2008 does that bring any interesting additional semantics you care about? |
To quote @AnthonyDGreen when responding to a complaint about
In short, that's an absolutely awful idea. In my opinion, |
Link to Docs string s = nothing; // (s == null) == true or the default value of the type.
Int i = nothing; // ( s == 0 ) = true or the default value of the type. I think that comment is about the expression
There is a quirk in VB.net |
Is there any difference between those semantics and the semantics of "default(T)" where T is whatever type is present? |
In other words, i see 'default' as just being shorthand for "default(T)". Are there additional VB semantics you see on top of that. |
@CyrusNajmabadi That's how I also see it, shorthand for |
Ok, yes, I agree. I makes sense for |
I hope that's because Erik Meijer had something to do with the implementation. |
@AdamSpeight2008 Ah ok. I would not want it to be like VB's Nothing then. I would prefer it be like C#'s default(T). |
I agree with others here who have questioned why dynamic x = default; // error, type isn't specified
var x = default; // error, type isn't specified
ITest i = default; // OK: default is shorthand for default(ITest) |
@DavidArno There was no LDM discussion of this feature yet. |
Per your feedback, I have changed the prototype to allow |
This is feature complete, as far as I can tell. No known work left. Completed a manual test pass in IDE. |
Work items:
as
andis
scenarios.Test ideas:
dynamic x = default;
// errorconst int x = default;
// okvar x = default;
// error(int, int) t = (default, default);
// okint[] t = new[] { default };
// okITest i = default;
// error@default
default.ToString();
M(default);
wherevoid M<T>(T x) { ... }
// errorflag ? default : 1;
default
in enum?M(default, 1)
,M(default, (1, 2))
const object x = default(S);
C[] x = new[] { default };
null == default
LDM questions:
var x = (int)default;
(I think the answer is yes).throw default;
? (current behavior is no)default
has a type (which is inferred) and also a constant value (if appropriate)case default:
give a warning (LDM 3/7)Relates to language proposals #7737 and #13255
Overload resolution might re-use some techniques from #11493
The text was updated successfully, but these errors were encountered: