Facilitates the use of inline type parameters. For non-inline use cases, refer to TypeTemplate.create.
Note that the complexity of this approach arises from the F# limitation that doesn't allow defining functions with type parameters within another function or method. Using an interface serves as a workaround.
Suppose we have an object of type obj
. Let's define it for illustrative purposes:
let myObj = box "hi"
myObj, [myObj]
, it would typically require tedious reflection. With ITypeTemplate
, this can be achieved as:
let myNewObj =
{ new ITypeTemplate<obj> with
member _.Def<'t>() =
let t = myObj :?> 't
t, [t]
}.Make [myObj.GetType()]
ITypeTemplate<obj>
since the actual returned type is obj
(the type of myNewObj
). The Def<'t>
method defines a type parameter to work with (the actual type of myObj
). Although myNewObj
is of type obj
, it can be used in functions/methods that accept a string * (string list)
tuple.
Instance member | Description |
Full Usage:
this.Def
Returns: 'b
Modifiers: abstract |
|