This module defines compile-time reflection procs for working with types.
Unstable API.
Types
StaticParam[value] = object
- used to wrap a static value in genericParams Source Edit
Procs
proc name(t: typedesc): string {...}{.magic: "TypeTrait".}
-
Returns the name of the given type.
Alias for system.`$`(t) since Nim v0.20.
Source Edit proc arity(t: typedesc): int {...}{.magic: "TypeTrait".}
-
Returns the arity of the given type. This is the number of "type" components or the number of generic parameters a given type t has.
Example:
assert arity(seq[string]) == 1 assert arity(array[3, int]) == 2 assert arity((int, int, float, string)) == 4
Source Edit proc genericHead(t: typedesc): typedesc {...}{.magic: "TypeTrait".}
-
Accepts an instantiated generic type and returns its uninstantiated form.
For example:
- seq[int].genericHead will be just seq
- seq[int].genericHead[float] will be seq[float]
A compile-time error will be produced if the supplied type is not generic.
See also:
Example:
type Functor[A] = concept f type MatchedGenericType = genericHead(typeof(f)) # `f` will be a value of a type such as `Option[T]` # `MatchedGenericType` will become the `Option` type
Source Edit proc stripGenericParams(t: typedesc): typedesc {...}{.magic: "TypeTrait".}
- This trait is similar to genericHead, but instead of producing error for non-generic types, it will just return them unmodified. Source Edit
proc supportsCopyMem(t: typedesc): bool {...}{.magic: "TypeTrait".}
-
This trait returns true if the type t is safe to use for copyMem.
Other languages name a type like these blob.
Source Edit proc isNamedTuple(T: typedesc): bool {...}{.magic: "TypeTrait".}
- Return true for named tuples, false for any other type. Source Edit
proc distinctBase(T: typedesc): typedesc {...}{.magic: "TypeTrait".}
- Returns base type for distinct types, works only for distinct types. compile time error otherwise Source Edit
proc tupleLen(T: typedesc[tuple]): int {...}{.magic: "TypeTrait".}
- Return number of elements of T Source Edit
Templates
template distinctBase[T](a: T): untyped
-
overload for values
Example:
type MyInt = distinct int doAssert 12.MyInt.distinctBase == 12
Source Edit template tupleLen(t: tuple): int
- Return number of elements of t Source Edit
template get(T: typedesc[tuple]; i: static int): untyped
- Return ith element of T Source Edit
template elementType(a: untyped): typedesc
-
return element type of a, which can be any iterable (over which you can iterate)
Example:
iterator myiter(n: int): auto = for i in 0..<n: yield i doAssert elementType(@[1,2]) is int doAssert elementType("asdf") is char doAssert elementType(myiter(3)) is int
Source Edit template genericParams(T: typedesc): untyped
-
return tuple of generic params for generic T
Example:
type Foo[T1, T2] = object doAssert genericParams(Foo[float, string]) is (float, string) type Bar[N: static float, T] = object doAssert genericParams(Bar[1.0, string]) is (StaticParam[1.0], string) doAssert genericParams(Bar[1.0, string]).get(0).value == 1.0 doAssert genericParams(seq[Bar[2.0, string]]).get(0) is Bar[2.0, string] var s: seq[Bar[3.0, string]] doAssert genericParams(typeof(s)) is (Bar[3.0, string],) # NOTE: For the builtin array type, the index generic param will # **always** become a range type after it's bound to a variable. doAssert genericParams(array[10, int]) is (StaticParam[10], int) var a: array[10, int] doAssert genericParams(typeof(a)) is (range[0..9], int)
Source Edit