Replies: 10 comments
-
I'd second this. I've recently added Sadly I suspect there isn't time to "fix" this before v8 ships and once shipped, it would become a breaking change to invert it. So we are likely stuck with this. |
Beta Was this translation helpful? Give feedback.
-
A lot of generic code without any constraints is type oblivious. The classic cases such as collections, Linq etc. all work on any type, including null. So I think this is the correct default for nullable code. |
Beta Was this translation helpful? Give feedback.
-
The default state for nullable/not nullable once NRTs is enabled is not nullable. And to enable the former, one just needs I've just worked through a library of only a few thousand lines of code that uses a lot of generics. Having made the decision in one place that the generic type must not be nullable, I then had to add around 300 So to my mind, the team got this one completely wrong. |
Beta Was this translation helpful? Give feedback.
-
I'm thinking that the premise is that the generic code usually doesn't care about the type (e.g. Linq) -- nullable-in, nullable-out, and vice versa. so they need to be explicitly "constrained" to be not null. I haven't palyed with NRT that much yet, @DavidArno could you give a example where |
Beta Was this translation helpful? Give feedback.
-
The simplest example I can come up with is this one: #nullable enable
class C1<T>
{
public string M1(T x) => x.ToString();
} I can add a #nullable enable
class C1<T> where T : notnull
{
public string M1(T x) => x.ToString();
}
class C2<T>
{
private C1<T> _x;
} then the compiler insists I add that |
Beta Was this translation helpful? Give feedback.
-
Yeah, if it was in reverse e.g. |
Beta Was this translation helpful? Give feedback.
-
That is true of any constraint on generics. By default, a generic allows all types. Constraints restrict that, and are inherently viral. The question isn't "is this constraint viral?". It's "which is more common, to have this constraint or not to". Also I would point out it would be unique to have a |
Beta Was this translation helpful? Give feedback.
-
@DavidArno Though I'm yet to see a real-world example of this, I hardly recall calling ToString on an unconstrained generic type. Note that if you have another type constraint it is actually non-null by default. class C
{
public string M1<T>(T x) where T : C => x.ToString();
} |
Beta Was this translation helpful? Give feedback.
-
@alrz, in realty, my use cases are around using So I'm now trying to remove all those |
Beta Was this translation helpful? Give feedback.
-
@DavidArno, another way would be to use |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
https://devblogs.microsoft.com/dotnet/try-out-nullable-reference-types
If I prefer to avoid
null
-s in most cases and do<Nullable>enable</Nullable>
as well, will I still need to decorate all of my generics withnotnull
constraints rather than occasionally opting out via e.g.null
constraint?Beta Was this translation helpful? Give feedback.
All reactions