Skip to content

Instantly share code, notes, and snippets.

Created February 28, 2011 21:04
Show Gist options
  • Save anonymous/848035 to your computer and use it in GitHub Desktop.
Save anonymous/848035 to your computer and use it in GitHub Desktop.
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
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