DavidArno / SuccincT

Discriminated unions, pattern matching and partial applications for C#
MIT License
266 stars 15 forks source link

CaseOf<T> support for arbitrary types #22

Closed megafinz closed 7 years ago

megafinz commented 7 years ago

Is it possible to make General Pattern Matcher to be able to match objects on types apart from values just like Union Pattern Matcher does with it's union cases (via CaseOf)?

abstract class ItemBase { }
class ItemA : ItemBase { }
class ItemB : ItemBase { }
class ItemC : ItemBase { }

ItemBase GetItem() { … }

var item = GetItem();

item.Match()
    .WithType<ItemA>().Do(a => …)
    .WithType<ItemB>().Do(b => …)
    .WithType<ItemC>().Do(c => …)
    .Exec();

I'm not really sure this is constructive, though, because C# 7.0 can pattern match types with the same purpose (via swtich).

DavidArno commented 7 years ago

I agree there's little point in implementing this for Match ... Exec statements, as the C# 7 type matching additions to switch would cover this. However, there is likely benefit to adding Match .. Result expression support in the way you describe. We'll have to wait until at least C# v7.1 for the match expression equivalent.

DavidArno commented 7 years ago

v2.3.0 released.