public void BindDemo() { // Int.Parse is a function from LaYumba. It takes a string and returns an Option<int>. // Int.Parse: s -> Option<int> var optI = Int.Parse("12"); // Age.Of: int -> Option<Age> // Age.Of(1) // Combination with Map: var ageOpt = optI.Map(i => Age.Of(i)); // Problem: returns Option<Option<Age>> ARRGH! var ageOpt1 = optI.Map(i => Age.Of(i)); // Solution: Bind instead of Map when combining functions which return M<T> Func <string, Option <Age> > parseAge = s => Int.Parse(s).Bind(Age.Of); var ageO = parseAge("12"); ageO.Match( () => true.Should().Be(false), // <- ensure that this path is never called! x => x.Value.Should().Be(12) ); }
public void Age_has_smart_ctor() { // TODO Create readable test extension for Option Age.Of(10).Match( () => true.Should().Be(false), age => age.Value.Should().Be(10)); Age.Of(-1).Match( () => true.Should().Be(true), age => age.Should().Be(null)); }
public void Using_a_smart_ctor_works() { var optAge = Age.Of(10); optAge.Should().BeOfType <Option <Age> >(); }