Qit


ITypeTemplate<'b> Type

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.

Example

Suppose we have an object of type obj. Let's define it for illustrative purposes:

 let myObj = box "hi"
val myObj: obj
val box: value: 'T -> obj
If we wish to create a function that produces a typed tuple of 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()]
val myNewObj: obj
type obj = System.Object
We use 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 members

Instance member Description

this.Def

Full Usage: this.Def

Returns: 'b
Modifiers: abstract
Returns: 'b