4 ms·
> No one seems to know why C# behaves the way it does. Operators are lifted[0] over nullability in C#. Every value type T can be converted implicitly to Nullab
by chrisoverzero 8y ago
> No one seems to know why C# behaves the way it does.
Operators are lifted[0] over nullability in C#. Every value type T can be converted implicitly to Nullable<T>. When `10 * null` is typechecked, both sides of * are typed as Nullable<T>. The * operator then acts like the pseudo-Haskell
(*) <$> 10 <*> null
or, I guess
liftA2 (*) (Just 10) Nothing
The semantics of Nullable<T> are similar to those of an optional type, with some implicit mapping and lifting. In that context, null acts less like null. (Thanks to convenient conversions.) Note that the following doesn't throw:
int? value = null;
value.HasValue // == false
[0]: https://blogs.msdn.microsoft.com/ericlippert/2007/06/27/what-exactly-does-lifted-mean/ https://blogs.msdn.microsoft.com/ericlippert/2007/06/27/what...
- juliangoldsmith 8y agoI came here to write this. What C# does in this case makes sense if you take the time to pick apart what it's actually doing.