Funicular-Switch icon indicating copy to clipboard operation
Funicular-Switch copied to clipboard

Proposal: Add intermediate monadic types for option and generated results e.g. OptionNone with implicite conversion to Option<T> to enhance type inference

Open Tyrrx opened this issue 1 year ago • 2 comments

C# has very limited type inference. An intermediate monadic type representation would support C#'s type inference. For instance returning a OptionNone from Option.None that will be implicitly converted to an Option<T> as soon as it is passed to an already strong typed method. This could be just an extension as Option.None<T> could still return Option<T>.

// struct Option<T>: 
public static implicit operator Option<T>(OptionNone value) =>
            Option<T>.None;

Tyrrx avatar Mar 25 '24 09:03 Tyrrx

Hi, could you provide an example, where this type would help?

ax0l0tl avatar Apr 18 '24 06:04 ax0l0tl

Easiest example would be:

Option<int> opt = Option.Some(0);
Option<int> maybeOne = opt.Bind(one => one == 1 ? Option.Some(one) : Option.None);
// vs
Option<int> maybeOne = opt.Bind(one => one == 1 ? Option.Some(one) : Option.None<int>());

Tyrrx avatar Apr 19 '24 09:04 Tyrrx