Replies: 6 comments
-
I can imagine this being an opt-in feature that creates a wave of easily resolvable warnings. Don't think there'd be any breaking changes -- while providing an easy opportunity for a developer to harden their application against NullReferenceExceptions -- without changing syntax. |
Beta Was this translation helpful? Give feedback.
-
Please take language feature discussion to the csharplang mailing list. Thanks! Mads |
Beta Was this translation helpful? Give feedback.
-
This is now the right place for language feature discussion. |
Beta Was this translation helpful? Give feedback.
-
Yes. The original proposal way back on CodePlex was to keep un-annotated reference types nullable and use
Did you even read the design document? 🙃
|
Beta Was this translation helpful? Give feedback.
-
Re: The design doc itself:
Why not have it so that methods returning open generic types like these are adorned with the attribute indicating that their return value can be void Foo(IEnumerable<int> ints, IEnumerable<string> strings)
{
var i = ints.FirstOrDefault().GetHashCode(); //collection is of a value type, so no warning
var s = strings.FirstOrDefault().GetHashCode(); //warning: 'FirstOrDefault()' would return 'null' if the collection was empty
} |
Beta Was this translation helpful? Give feedback.
-
Here are the recent threads on the topic: Backwards compatibility is a primary requirement for the feature. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm curious if an alternate syntax has been explored, and if so the reasons for rejecting it. Specifically that would be using the "dammit" operator to specify that a type is non-nullable:
This provides a backwards compatible forward path, with no surprises for developers not expecting this change. In the future the "for now" case could be migrated from the second case to the third.
This could also happen in the generics case:
This would lead to somewhat of an explosion of dammits (e.g.
HashMap<String!, List<String!>!>!
), but would provide some clarity and consistency in the intent. It should also reduce confusion where a type parameter is nullable and non-nullable.At least addressing this in the proposal would be useful in answering this question before it comes up.
Beta Was this translation helpful? Give feedback.
All reactions