Logo Xantham

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
'T
'ReturnType
val object: 'T option (requires member ofValue)
val unbox: value: objnull -> 'T
val object: 'T (requires member ofValue)
'T: (member ofValue: 'ReturnType)

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
type TestObject = { ofValue: int }
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
type TestObject2 = { ofValue: string }
Multiple items
val string: value: 'T -> string

--------------------
type string = System.String
val testObject: TestObject
val testObject2: TestObject2
val ofValueAccessor: obj

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

this.Invoke

Full Usage: this.Invoke

Parameters:
    obj : ^T

Returns: ^ReturnType
Modifiers: inline
obj : ^T
Returns: ^ReturnType

this.Value

Full Usage: this.Value

Returns: string
Modifiers: inline

The string representation of the path.

Returns: string

Static members

Static member Description

typekeyof.Access(accessedObject, key)

Full Usage: typekeyof.Access(accessedObject, key)

Parameters:
    accessedObject : ^T
    key : typekeyof<^T, ^ReturnType>

Returns: ^ReturnType
Modifiers: inline

Access a property of an object using a path.

accessedObject : ^T
key : typekeyof<^T, ^ReturnType>
Returns: ^ReturnType

typekeyof.UnsafeAccess(accessedObject, key)

Full Usage: typekeyof.UnsafeAccess(accessedObject, key)

Parameters:
    accessedObject : obj
    key : typekeyof<'a, ^ReturnType>

Returns: ^ReturnType
Modifiers: inline

Access a property of an object without type checking the object type. UNSAFE.

accessedObject : obj
key : typekeyof<'a, ^ReturnType>
Returns: ^ReturnType

typekeyof.UnsafeCastFrom(value)

Full Usage: typekeyof.UnsafeCastFrom(value)

Parameters:
Returns: typekeyof<^T, ^ReturnType>
Modifiers: inline

Convert a path key for a different type to the given type. UNSAFE.

value : typekeyof<obj, ^ReturnType>
Returns: typekeyof<^T, ^ReturnType>

Type something to start searching.