-
Couldn't load subscription status.
- Fork 4
Open
Labels
Description
I just ran into a footgun with our implicit option constructor:
The following snippet does not return an empty option but a some-option with a default-initialized tuple inside:
Option<(string, int)> DefaultUsage(bool cond) => cond ? ("hello world", 42) : default;This of course applies to all value types:
Option<int> DefaultUsage() => false ? 42 : default; // returns Some(0), not None.for reference types, we get at least a nullable warning:
Option<string> DefaultUsage() => false ? "hello world" : default;
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// warning CS8604: Possible null reference argument for parameter 'item'
// in 'Option<string>.implicit operator Option<string>(string item)'.We should emit a warning in these cases:
defaultinside ternary inside implicit conversion toOptiondefaultinside switch expression inside implicit conversion toOption- others?
This is of course perfectly fine and returns None as expected:
Option<(string, int)> DefaultUsageFine(bool cond)
{
if (cond) { return ("hello world", 42); }
else { return default; }
}