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/0a3b36118cb3a3dce13e to your computer and use it in GitHub Desktop.
Save Mr-Byte/0a3b36118cb3a3dce13e 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);
//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