Created
February 9, 2016 03:38
-
-
Save theunrepentantgeek/b74b7b54d6535a6f8a8d to your computer and use it in GitHub Desktop.
This file contains 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; | |
namespace Immutable | |
{ | |
enum Qux { Qux1, Qux2 } | |
class Baz | |
{ | |
private Qux _qux; | |
public Baz(Qux qux) | |
{ | |
_qux = qux; | |
} | |
public Baz(Baz original) : this(original.Qux) | |
{ | |
} | |
public Baz WithQux(Qux qux) | |
{ | |
var result = new Baz(this); | |
result._qux = qux; | |
return result; | |
} | |
public Qux Qux { get { return _qux; } } | |
} | |
class Bar | |
{ | |
private Baz _baz; | |
public Bar(Baz baz) | |
{ | |
_baz = baz; | |
} | |
public Bar(Bar original) : this(original.Baz) | |
{ | |
} | |
public Bar WithBaz(Baz baz) | |
{ | |
var result = new Bar(this); | |
result._baz = baz; | |
return result; | |
} | |
public Bar WithQux(Qux qux) | |
{ | |
return WithBaz(Baz.WithQux(qux)); | |
} | |
public Baz Baz { get { return _baz; } } | |
} | |
class Foo | |
{ | |
private Bar _bar; | |
public Foo(Bar bar) | |
{ | |
_bar = bar; | |
} | |
public Foo(Foo original) : this(original.Bar) | |
{ | |
} | |
public Foo WithBar(Bar bar) | |
{ | |
var result = new Foo(this); | |
result._bar = bar; | |
return result; | |
} | |
public Foo WithBaz(Baz baz) | |
{ | |
return WithBar(Bar.WithBaz(baz)); | |
} | |
public Foo WithQux(Qux qux) | |
{ | |
return WithBar(Bar.WithQux(qux)); | |
} | |
public Bar Bar { get; } | |
} | |
static class Program | |
{ | |
private static Foo SetQux(Foo foo, Qux qux) => foo.WithBar(foo.Bar.WithBaz(foo.Bar.Baz.WithQux(qux))); | |
public static void Main() | |
{ | |
var it = new Foo(new Bar(new Baz(Qux.Qux1))); | |
Console.WriteLine(it.Bar.Baz.Qux); | |
var newIt = it.WithQux(Qux.Qux2); | |
Console.WriteLine(newIt.Bar.Baz.Qux); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment