Replies: 17 comments
-
There is a little overlapping between non-nullable reference type void F(string s1!, string s2!, string s3!, string? nullableS4) { } It would be obvious that the [CheckNulls]
void F(string s1, string s2, string s3, string? nullableS4) {}
// or
void F(string s1, string s2, string s3, string? nullableS4)
checked null
{
} And this will be more inline with code contracts, if they will ever be in. |
Beta Was this translation helpful? Give feedback.
-
Note, they apply at different times. The type string? is a compile-time clue to flow analysis. At run-time it's just a plain old string reference type, nulls and all. The parameter |
Beta Was this translation helpful? Give feedback.
-
I know that. My point is that, when written at declaration site, for code readers, they convey duplicated and potentially conflicting message. E.g. void F(string s1!, string s2, string s3!, string? nullableS4) { } // what happened to s2? I'm trying to remove that duplication, while still keeping them as separate features, by writing whether a parameter is expected to be null only once. |
Beta Was this translation helpful? Give feedback.
-
If I may, let's take as read the statement, "NRT will be great for new code and will be hard to apply to existing code". It's a sweeping generalisation, but if we assume it's true then Having then got the code working reliably with no NRE's using Obviously I've made a sweeping generalisation, and there will be existing code that can be switched to NRTs easily. But for code that fits the generalisation, I agree with you though that there seems little point mixing both auto-generated null checks and NRTs. An exception possibly being libraries that use NRTs but also want to maintain compatibility with code that doesn't. |
Beta Was this translation helpful? Give feedback.
-
It's an edge case but when the shape is millions of developers in size, the edge is large... |
Beta Was this translation helpful? Give feedback.
-
Can I have some clarification on this? What does "stub" stand for, in this situation? public static IEnumerable<T> M<T>(this IEnumerable<T> source!)
{
IEnumerable<T> m()
{
//null check here?
foreach (var item in source)
yield return item;
}
//or here?
return m();
} |
Beta Was this translation helpful? Give feedback.
-
I took it to mean the second location, in that the iterator method would throw immediately and not on the first attempt to |
Beta Was this translation helpful? Give feedback.
-
@qrli With the damnit operator you are more flexible, especially when unconstrained generics are supported. Consider the following: public void IDictionary<TKey, TValue>.Add(TKey key!, TValue value)
{
...
} It's not clear if a |
Beta Was this translation helpful? Give feedback.
-
And it is shorter for lambdas:
vs. o checked null => o.Name |
Beta Was this translation helpful? Give feedback.
-
@quinmars Per my understanding, using That being said, your point is uncontrained generic parameter, where the type cannot be treat and non-nullable but the parameter expects to be non-null. I agree this is a possible scenario. Nitpicking: I feel your example, |
Beta Was this translation helpful? Give feedback.
-
To be clear, I'm not against the feature. I really love this feature. |
Beta Was this translation helpful? Give feedback.
-
Well, it is how https://referencesource.microsoft.com/#mscorlib/system/collections/generic/dictionary.cs,319 |
Beta Was this translation helpful? Give feedback.
-
Rethought over:
Now, I'm inclined to the M(string? s) // s is nullable
M(string s) // s is non-nullable but no check
M(string! s) // s is non-nullable and do check!
// In addition, s cannot be reassigned to null if NRT is turned on. And this seems to also smooth out the unconstrained generic parameter case. And we can enable non-nullable for unconstrained generic parameter: N<T>(T! v1, T v2) // v1 becomes non-nullable type, while v2 may be nullable |
Beta Was this translation helpful? Give feedback.
-
Yes, the JIT already removes null checks against value types in open generic types/methods, nothing new needs to happen in that regard. |
Beta Was this translation helpful? Give feedback.
-
But the JIT will remove null check for "string x" too if NRT is enabled? |
Beta Was this translation helpful? Give feedback.
-
@fanol |
Beta Was this translation helpful? Give feedback.
-
@Logerfo @HaloFour Correct, we will throw before constructing the enumerator. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
https://github.com/dotnet/csharplang/blob/master/meetings/2019/LDM-2019-01-14.md
Agenda
parameter!
Proposal: Proposal: Simplified parameter null validation #2144
Beta Was this translation helpful? Give feedback.
All reactions