Last active
January 18, 2019 10:09
-
-
Save isaacabraham/a2b818b209ff15fc1f502cd8080a31c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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