I'm trying to replicate some light Option\
Consider the code:
public abstract class Option<T> { }
public class None<T> : Option<T> { }
public class Some<T> : Option<T> {
public T Value { get; }
public Some(T value) => Value = value;
}
If i try property pattern on Some\
static string Match(Option<Foo> maybeFoo) =>
maybeFoo switch
{
// works
None<Foo> _ => "Theres nothing",
// works
Some<Foo> someFoo when someFoo.Value.Bar > 42 => someFoo.Value.Baz,
// Syntax error
Some<Foo> { Value: var p } when p.Bar == 42 => p.Baz,
_ => string.Empty,
};
If I define a Type alias to Some\
using SomeFoo = Program.Some<Foo>;
static string Match(Option<Foo> maybeFoo) =>
maybeFoo switch
{
// works
None<Foo> _ => "Theres nothing",
// now works
SomeFoo { Value: var p } when p.Bar == 42 => p.Baz,
_ => string.Empty,
};
Have you tried adding a deconstructor?
@orthoxerox yeah, works with deconstructor.
Like this
https://github.com/dotnet/csharplang/issues/883#issuecomment-513598593
This code fails to compile, because the rules used by the compiler to decide between interpreting <> as generic type markers and comparison operators didn't take property patterns into account.
I have submitted https://github.com/dotnet/roslyn/pull/37898 in an attempt to fix that.
@svick I am just back from vacation. I will look at this tomorrow.