Last active
August 29, 2015 14:05
-
-
Save Mr-Byte/0a3b36118cb3a3dce13e 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); | |
//The following line yields the error: option.cs(37,12): error CS0117: 'Option.None' does not contain a definition for 'is operator' | |
if(y is None()) | |
{ | |
Console.WriteLine("None"); | |
} | |
else | |
{ | |
Console.WriteLine(y); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment