Created
September 12, 2017 17:31
-
-
Save phillipsj/65a89b3c272880a7429a57e68b3430b3 to your computer and use it in GitHub Desktop.
OptionWithMapt
This file contains hidden or 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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Linq.Expressions; | |
using System.Security.Cryptography.X509Certificates; | |
using option; | |
namespace option { | |
class Program { | |
static void Main(string[] args) { | |
string message; | |
var option = GetOption(true); | |
Console.WriteLine(EvaulateOption(option)); | |
var options = GetListOfOptions(); | |
var somes = options.OfType<Some<string>>().Select(x => x.Value); | |
foreach (var some in somes) { | |
Console.WriteLine(some); | |
} | |
Console.ReadLine(); | |
} | |
static IEnumerable<Option> GetListOfOptions() { | |
return new List<Option> { | |
new None(), | |
new Some<string>("hi"), | |
new Some<string>("hello"), | |
new Some<string>("hey") | |
}; | |
} | |
static Option GetOption(bool ifSome) { | |
return ifSome ? new Some<string>("Hello!") : new None() as Option; | |
} | |
static string EvaulateOption(Option option) { | |
switch (option) { | |
case Some<string> s: | |
return s.Value; | |
case None n: | |
return "None"; | |
default: | |
return "Default"; | |
} | |
} | |
} | |
abstract class Option { | |
} | |
class Some<T> : Option { | |
public T Value { get; } | |
public Some(T value) { | |
Value = value; | |
} | |
} | |
class None : Option { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment