typekeyof<'T, 'ReturnType> Type
Equivalent to keyof but keeps the return type.
This type can be used to create a SRTP constraint for a property key of an unknown type.
The following example creates an SRTP type to reflect the a typed accessor for any object with a member 'ofValue'
type OfValueAccessor =
static member inline create<
^T, ^ReturnType when ^T:(member ofValue: ^ReturnType)
>(?object: ^T): typekeyof<^T, ^ReturnType>
= unbox "ofValue"
static member inline access<
^T, ^ReturnType when ^T:(member ofValue: ^ReturnType)
>(object: ^T): typekeyof<^T, ^ReturnType>
= object.ofValue
Now we can observe usage. If we were to create an untyped ofValue typekeyof by using
the unit method call, then the result type will be typekeyof<obj, obj>
Subsequent usage would instantly resolve the type to the object it is used on, but this would invalidate it for usage on other types with the same property.
type TestObject = {
ofValue: int
}
type TestObject2 = {
ofValue: string
}
let testObject = { TestObject.ofValue = 1 }
let testObject2 = { TestObject2.ofValue = "1" }
let ofValueAccessor = OfValueAccessor.create()
// the line below will resolve ofValueAccessor to typekeyof<TestObject, int>
TypeKeyOf.access ofValueAccessor testObject
// The line before can no longer be used on testObject2, as it is of a different type
// TypeKeyOf.access ofValueAccessor testObject2
val int: value: 'T -> int (requires member op_Explicit)
--------------------
type int = int32
--------------------
type int<'Measure> = int
val string: value: 'T -> string
--------------------
type string = System.String
We can also alternatively use the SRTP static method to access the value directly, without restricting the method to a single object type.
// Both are valid and are resolved by the compiler correctly
OfValueAccessor.access testObject // int
OfValueAccessor.access testObject2 // string
Naturally, this is standard usage of SRTP that can be done by hand anytime, and is nothing unique to Xantham. Providing these types and tools in the library is done to allow the Xantham generators to refer to these types, or to create these SRTP types during their generation steps.
Instance members
| Instance member |
Description
|
Full Usage:
this.Invoke
Parameters:
^T
Returns: ^ReturnType
Modifiers: inline |
|
Full Usage:
this.Value
Returns: string
Modifiers: inline |
The string representation of the path.
|
Static members
| Static member |
Description
|
Full Usage:
typekeyof.Access(accessedObject, key)
Parameters:
^T
key : typekeyof<^T, ^ReturnType>
Returns: ^ReturnType
Modifiers: inline |
Access a property of an object using a path.
|
Full Usage:
typekeyof.UnsafeAccess(accessedObject, key)
Parameters:
obj
key : typekeyof<'a, ^ReturnType>
Returns: ^ReturnType
Modifiers: inline |
Access a property of an object without type checking the object type. UNSAFE.
|
|