Skip to content

Instantly share code, notes, and snippets.

@Mr-Byte
Last active August 29, 2015 14:05
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 Mr-Byte/40e8abb593451ccce2ec to your computer and use it in GitHub Desktop.
Save Mr-Byte/40e8abb593451ccce2ec to your computer and use it in GitHub Desktop.
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