Skip to content

Instantly share code, notes, and snippets.

@phillipsj
Created September 12, 2017 17:31
Show Gist options
  • Save phillipsj/65a89b3c272880a7429a57e68b3430b3 to your computer and use it in GitHub Desktop.
Save phillipsj/65a89b3c272880a7429a57e68b3430b3 to your computer and use it in GitHub Desktop.
OptionWithMapt
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