Skip to content

Instantly share code, notes, and snippets.

@danclien
Last active April 8, 2016 21:37
Show Gist options
  • Save danclien/9adc6d20dc88d28c05e90df4e3f05719 to your computer and use it in GitHub Desktop.
Save danclien/9adc6d20dc88d28c05e90df4e3f05719 to your computer and use it in GitHub Desktop.
// data Foo = Bar Int | Baz String
public abstract class Foo
{
public sealed class Bar : Foo
{
public int Value { get; set; }
}
public sealed class Baz : Foo
{
public string Value { get; set; }
}
public T Fold<T>(Func<Bar, T> barFunc, Func<Baz, T> bazFunc)
{
if (this is Bar)
{
return barFunc((Bar) this);
}
else if (this is Baz)
{
return bazFunc((Baz) this);
}
else
{
throw new Exception("Oops");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment