Replies: 31 comments
-
|
Beta Was this translation helpful? Give feedback.
-
These cases don't clearly demonstrate why this is a disadvantage. The first example is better for not allowing |
Beta Was this translation helpful? Give feedback.
-
With nullable reference types on the way, even if such an idea were adopted, then that default type would need to be But leaving that aside, I agree with @bondsbw: your |
Beta Was this translation helpful? Give feedback.
-
@DavidArno @bondsbw I think that should not be a responsible for function to help us avoid null like that. It fine if it translate to The example just demonstrate what kind of code that error. The advantage is as I said above, serializing and interop with other system. Such as I was working with JSON and GoogleProtoBuf. Both allow serialize null value public static Google.ProtoBuf.Value CustomSerializeValue<T>(T value)
{
if(value == null)
return new Google.ProtoBuf.Value() { NullValue = true };
// Do something with T value
return Google.ProtoBuf.ValueSerializer.Serialize(value);
} The actual code I written are more complex but in the end I just want to write |
Beta Was this translation helpful? Give feedback.
-
Couldn't just |
Beta Was this translation helpful? Give feedback.
-
That was my initial thought too. But where does it fit in the type hierarchy and how would the language and runtime cope with casting between "NullType" and all other types (taking into account invariant collections etc)? I suspect the cons far outweigh the pros on this one. |
Beta Was this translation helpful? Give feedback.
-
@GeirGrusom so should |
Beta Was this translation helpful? Give feedback.
-
Is "this code used to crash; now it works!" really a breaking change? 😀 With the move to nullable ref types, and the near inevitable adoption of |
Beta Was this translation helpful? Give feedback.
-
I’m would not be surprised if somebody out there somewhere has a function like: static bool IsNull(object o)
{
try
{
o.ToString();
}
catch (NullReferenceException)
{
return false;
}
return true;
} |
Beta Was this translation helpful? Give feedback.
-
@DavidArno Well, it is. There could be a logic that rely on @yaakov-h I don't expect And when it was check for |
Beta Was this translation helpful? Give feedback.
-
Can't you specialize like this?
Then you can just do exactly what you want without casting?
Alternatively,
About the tuple I don't know maybe, although, I agree with @yaakov-h on this and the docs state this exactly:
|
Beta Was this translation helpful? Give feedback.
-
"but some folk write super weird code..." 😁 In my view, anyone writing such horrific code as that deserves to have it break. It might teach them a lesson in how to write better code that way 😆 |
Beta Was this translation helpful? Give feedback.
-
I agree, but can you convince the LDM? |
Beta Was this translation helpful? Give feedback.
-
@eyalsk Well, I just think about this issue in the same time as Tuple so if we would fix I think it should be fixed together Yes, we can do as you have shown but it would be unnecessary if it just allowed to do. And it was a bug prone to have boilerplate code to maintain |
Beta Was this translation helpful? Give feedback.
-
@DavidArno As the law standard state that we should not punish people who do illegal thing in the past when the action was not yet defined as illegal. Coding standard should be the same |
Beta Was this translation helpful? Give feedback.
-
Since C# 1.0 the language specification has required that attempting to call an instance member of a And remember, non-nullable reference "types" does not introduce new types into the language. The type will always be the reference type, the (non-)nullability is a property of the variable and flow-analysis. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour I don't try to propose that calling any member of null value should return anything or not throw exception What this issue mean is compile time error when infer a var pair = ("key",null); // pair should just be Tuple<string,object?> |
Beta Was this translation helpful? Give feedback.
-
@DavidArno null already has a type. All we know about @yaakov-h |
Beta Was this translation helpful? Give feedback.
-
@GeirGrusom I think, maybe, null is a type in C# language spec since 1.0 strictly forbidden usage of type |
Beta Was this translation helpful? Give feedback.
-
Ah, I didn't know that. Thanks for the info. |
Beta Was this translation helpful? Give feedback.
-
@Thaina Any reason why I guess what I'm saying is
As opposed to something like in the example you gave |
Beta Was this translation helpful? Give feedback.
-
@eyalsk Linq and all generic extension method that could automatically infer type var objs = array.Select((item) => (key:item,value:null)); And using And also because sometime it not just var key = someThing.GetLongNameTypeKey();
(SomeType.WithVery.LongPath.AndLongName key, object value) pair = (key, null);
// vs
var pair = (key, null); |
Beta Was this translation helpful? Give feedback.
-
@Thaina |
Beta Was this translation helpful? Give feedback.
-
@GeirGrusom |
Beta Was this translation helpful? Give feedback.
-
If you want to pass null to generic function, use |
Beta Was this translation helpful? Give feedback.
-
Default is target-typed. I don’t believe you can use it in this context either.. |
Beta Was this translation helpful? Give feedback.
-
If you're using a generic, there's no reason to only ever pass in null. If you're only going to pass in null, don't make the function generic. If you aren't only going to pass in null, assuming the type of null to be object could cause even more problems with generic types not matching up. This just feels like it could cause a lot of issues when someone forgets to specify their generic and then ends up spending hours searching for why their program thinks they're trying to implicitly convert between object and whatever their type is. Typing in a few extra characters (and even fewer if you have good templates and autocomplete) seems like a fair price to make that not happen. |
Beta Was this translation helpful? Give feedback.
-
You would be discriminating all those poor |
Beta Was this translation helpful? Give feedback.
-
@IllidanS4 I don't think it discrimination because |
Beta Was this translation helpful? Give feedback.
-
(T, object) pair = ("key", null); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
It happen when I write generic method that can put any object to be serialized. And when I create tuple
I expect both case to accept
null
asobject
. But it error cannot be inferred from the usage insteadI think it could be useful if we could treat
null
as justobject
for inferring type in most case. Especially in serializing and interop with other systemBeta Was this translation helpful? Give feedback.
All reactions