Skip to content

Instantly share code, notes, and snippets.

@metametaclass
Created August 30, 2012 08:02
Show Gist options
  • Save metametaclass/3523947 to your computer and use it in GitHub Desktop.
Save metametaclass/3523947 to your computer and use it in GitHub Desktop.
f#
let inline getName arg =
( ^a : (member Name : string) arg )
//function type:
//val inline getName :
// ^a -> string when ^a : (member get_Name : ^a -> string)
let inline dumpNamed<'T when 'T : (member Name : string)> (arg:seq<'T>) =
arg |> Seq.iter (getName >> printfn "%s")
//val inline dumpNamed :
// seq< ^T> -> unit when ^T : (member get_Name : ^T -> string)
//record with get_Name member
type Test = {
_Name :string
}
with
member this.Name = this._Name
//record with get_Name member (generated by compiler!)
type Test1 = {
Name :string
}
let ts = [{_Name="n1"};{_Name="n2"};{_Name="n3"}]
let ts1 = [{Name="n11"};{Name="n12"};{Name="n13"}]
//type-check pass
dumpNamed ts
//type-check fails
//dumpNamed ts1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment