-
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
C# Design Notes for Mar 4, 2015 #1303
Comments
So if you guys aren't adding |
About the whole "what is the default value of a non-nullable reference type". Maybe say that only types that provide a default no-arg constructor can be used as non-nullable reference types. That would get around the problem of default value (if the no-arg constructor initialized the object correct). I know that this can make it confusing to know whether a reference type is in fact non-nullable but I believe we all use an IDE that can help with that. |
I think you mixed up WriteLine(n.Length); // Error!
WriteLine(s.Length); // Sure; it can't be null so no harm
if (n is string! ns) WriteLine(ns.Length); // Sure; you checked and dug out the value |
@Miista what does that solve? The only sane thing would be to require that all non-nullable fields are assigned in every constructor. If you have a non-nullable field the compiler cannot generate a default constructor, and it shouldn't. It has to require that the type has at least one defined constructor. If it is parameterless or not doesn't matter. In my opinion these should be the rules:
One advantage of a |
I agree about the requirement for at least one defined constructor (which also initializes all non-nullable fields). However if it's not parameterless what is the result of
Unless there is some requirement that non-nullable types declare a constructor that initializes all non-nullable fields.
I think this would be okay since the contract for
This was what I meant. |
Generics that has a class constraint can return null, which will fail in run-time at assignment, but they can also return T ReturnDefault<T>() where T : class
{
return default(T);
}
void Foo()
{
string! f = ReturnDefault<string!>();
Console.WriteLine(f.Length); // NRE
} Generics will have to be programmed with non-nullable types in mind. edit: There is probably a way to solve this issue for generic collections. How about this: public class List<T> where T : T! In this manner the compiler will actually enforce So |
Since the constraint is You can assign a non-nullable reference to a nullable reference but the reverse is not possible. The same is true here. The non-nullable reference satisfies the generic constraint however the constraint only promises that any output is most definitely a Søren Palmund
|
I don't agree with this.
It isn't immediately visible that non-nullable types are also acceptable. What this says when I read it is that no nullable reference types can be used. |
"The perfect is the enemy of the good" is absolutely right. Generics seem like a can of worms with non-nullability, especially with any existing generic type. The runtime simply could not enforce that For your own generic types how would |
I think the team should have a wait-and-see attitude with respect to non-nullability in the language because its such a difficult problem. The (Not that I don't think non-nullability has a lot of value, its just so difficult to add it to the language at this point.) |
@MgSam We will have an analyzer that goes with the Roslyn nuget packages that checks for clients attempting to extend or implement |
A common source of edit: if not-null in generics isn't implemented the code would still be wrong, but it wouldn't be wrong because the compiler allowed obviously wrong code. |
@SolalPirelli: I did indeed mix up |
Not sure what has been discussed about records here so far, but this seems like exactly the sort of problem that "type providers" (a la F#) can address. I'm not saying C# should be exactly compatible with F# type providers, but that's generally the gist. It's also useful to keep in mind why people want records in the first place: typically, there is some service endpoint which defines some contract/schema (a la WSDL or OData), and it often feels like boilerplate to have to then go and write a class to represent this schema manually. The lighter weight, the better, hence the desire for records. Luckily there's great support for WSDL in VS, but it's relatively heavy weight, and WSDLs aren't the only interfaces out there for clients to work against. Enter type providers. Given this other dimension of the problem, revisiting the idea of type providers for C# really kills two birds with one stone, and it would be a breakthrough language feature on the order of LINQ or async/await. Personally, this is what I'm rooting for. |
The design notes have been moved to https://github.com/dotnet/roslyn/blob/master/docs/designNotes/2015-03-04%20C%23%20Design%20Meeting.md Discussion on these design notes are in this issue. |
C# Design Meeting Notes for Mar 4, 2015
The design notes can be found at https://github.com/dotnet/roslyn/blob/master/docs/designNotes/2015-03-04%20C%23%20Design%20Meeting.md
Discussion on these design notes are in this issue.
The text was updated successfully, but these errors were encountered: