Last active
August 29, 2015 14:05
-
-
Save Mr-Byte/40e8abb593451ccce2ec 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 Option | |
{ | |
public abstract class Option | |
{ | |
public static Option Wrap<T>(T value) | |
where T : class | |
{ | |
if(ReferenceEquals(value, null)) | |
{ | |
return new None(); | |
} | |
return new Some<T>(value); | |
} | |
} | |
public record class None() : Option {} | |
public record class Some<T>(T value) : Option | |
{ | |
public override string ToString() | |
{ | |
return string.Format("Some({0})", value); | |
} | |
} | |
public class Program | |
{ | |
public static void Main() | |
{ | |
var x = "Hello, world!"; | |
var y = Option.Wrap(x); | |
if(y is Some(string value)) | |
{ | |
Console.WriteLine(value); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment