Created
February 28, 2011 21:04
-
-
Save anonymous/848035 to your computer and use it in GitHub Desktop.
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
type O<'T>(v : 'T, isSet : bool) = | |
struct | |
static member op_Implicit(value) : O<'T> = O(value, true) | |
member this.ValueOrDefault(defaultValue) = if isSet then v else defaultValue | |
end | |
module O = | |
let get (o : O<_>) v = o.ValueOrDefault v | |
type Optional = System.Runtime.InteropServices.OptionalAttribute | |
type Record = | |
{ | |
A : int | |
B : string | |
} | |
with | |
member this.With ([<Optional>] a, [<Optional>]b) = | |
{ | |
A = O.get a this.A | |
B = O.get b this.B | |
} | |
override this.ToString() = | |
sprintf "A=%d, B=%s" this.A this.B |
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
var a = new global::Program.Record(1, "2"); | |
Console.WriteLine(a); // A = 1, B = 2 | |
var b = a.With(b: "123"); | |
Console.WriteLine(b); // A = 1, B = 123 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment