Skip to content

Instantly share code, notes, and snippets.

@isaacabraham
Last active January 18, 2019 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacabraham/a2b818b209ff15fc1f502cd8080a31c7 to your computer and use it in GitHub Desktop.
Save isaacabraham/a2b818b209ff15fc1f502cd8080a31c7 to your computer and use it in GitHub Desktop.
using System;
namespace ConsoleApp1
{
abstract class Result {
public static Result Ok<T>(T value) => new Ok<T> { Value = value };
public static Result Error<T>(T value) => new Error<T> { Value = value };
}
sealed class Ok<T> : Result
{
public T Value { get; set; }
}
sealed class Error<T> : Result
{
public T Value { get; set; }
}
class Program
{
static void ParseResult(Result r)
{
switch (r)
{
case Ok<int> s:
Console.WriteLine($"SUCCESS! '{s.Value}'.");
break;
case Error<string> e:
Console.WriteLine($"ERROR! '{e.Value}'.");
break;
default:
Console.WriteLine("URGH.");
break;
}
}
static void Main(string[] args)
{
var good = Result.Ok(123);
ParseResult(good);
var bad = Result.Error("Oh no!");
ParseResult(bad);
var ugly = Result.Error(99); // shouldn't compile
ParseResult(ugly);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment