Skip to content

Instantly share code, notes, and snippets.

@t0yv0
Created September 9, 2010 13:35
Show Gist options
  • Save t0yv0/571858 to your computer and use it in GitHub Desktop.
Save t0yv0/571858 to your computer and use it in GitHub Desktop.
module Specialization =
type ITyped<'T1,'T2> =
abstract member Run<'T3> : 'T1 -> 'T2
type private Function<'T1,'T2> = delegate of 'T1 -> 'T2
let Specialize<'T1,'T2> (algorithm: ITyped<'T1,'T2>) =
let aT = typeof<ITyped<'T1,'T2>>
let def = aT.GetMethod("Run").GetGenericMethodDefinition()
let dT = typeof<Function<'T1,'T2>>
fun (t: System.Type) ->
let m = def.MakeGenericMethod [| t |]
let f = System.Delegate.CreateDelegate(dT, algorithm, m)
(f :?> Function<'T1,'T2>).Invoke
(*
Specialization.Specialize
{ new Specialization.ITyped<unit,string> with
member this.Run<'T>(_) =
typeof<'T>.Name
}
typeof<string>
()
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment