# typeinfo

- Manual
- Standard library
- Index

Search:
Group by:

This module implements an interface to Nim's runtime type information (RTTI). See the marshal module for an example of what this module allows you to do.

Note that even though `Any` and its operations hide the nasty low level details from its clients, it remains inherently unsafe! Also, Nim's runtime type information will evolve and may eventually be deprecated. As an alternative approach to programmatically understanding and manipulating types, consider using the macros package to work with the types' AST representation at compile time. See, for example, the getTypeImpl proc. As an alternative approach to storing arbitrary types at runtime, consider using generics.

# Types

    AnyKind = enum
      akNone = 0,                   ## invalid any
      akBool = 1,                   ## any represents a ``bool``
      akChar = 2,                   ## any represents a ``char``
      akEnum = 14,                  ## any represents an enum
      akArray = 16,                 ## any represents an array
      akObject = 17,                ## any represents an object
      akTuple = 18,                 ## any represents a tuple
      akSet = 19,                   ## any represents a set
      akRange = 20,                 ## any represents a range
      akPtr = 21,                   ## any represents a ptr
      akRef = 22,                   ## any represents a ref
      akSequence = 24,              ## any represents a sequence
      akProc = 25,                  ## any represents a proc
      akPointer = 26,               ## any represents a pointer
      akString = 28,                ## any represents a string
      akCString = 29,               ## any represents a cstring
      akInt = 31,                   ## any represents an int
      akInt8 = 32,                  ## any represents an int8
      akInt16 = 33,                 ## any represents an int16
      akInt32 = 34,                 ## any represents an int32
      akInt64 = 35,                 ## any represents an int64
      akFloat = 36,                 ## any represents a float
      akFloat32 = 37,               ## any represents a float32
      akFloat64 = 38,               ## any represents a float64
      akFloat128 = 39,              ## any represents a float128
      akUInt = 40,                  ## any represents an unsigned int
      akUInt8 = 41,                 ## any represents an unsigned int8
      akUInt16 = 42,                ## any represents an unsigned in16
      akUInt32 = 43,                ## any represents an unsigned int32
      akUInt64 = 44                 ## any represents an unsigned int64

  what kind of `any` it is Source Edit

    Any = object
      value: pointer
      when defined(js):
          rawType: PNimType

      else:
          rawTypePtr: pointer

  can represent any nim value; NOTE: the wrapped value can be modified with its wrapper! This means that `Any` keeps a non-traced pointer to its wrapped value and must not live longer than its wrapped value. Source Edit

# Procs

    proc toAny[T](x: var T): Any {...}{.inline.}

  constructs a `Any` object from `x`. This captures `x`'s address, so `x` can be modified with its `Any` wrapper! The client needs to ensure that the wrapper does not live longer than `x`! Source Edit

    proc kind(x: Any): AnyKind {...}{.inline, raises: [], tags: [].}

  get the type kind Source Edit

    proc size(x: Any): int {...}{.inline, raises: [], tags: [].}

  returns the size of `x`'s type. Source Edit

    proc baseTypeKind(x: Any): AnyKind {...}{.inline, raises: [], tags: [].}

  get the base type's kind; `akNone` is returned if `x` has no base type. Source Edit

    proc baseTypeSize(x: Any): int {...}{.inline, raises: [], tags: [].}

  returns the size of `x`'s basetype. Source Edit

    proc invokeNew(x: Any) {...}{.raises: [], tags: [].}

  performs `new(x)`. `x` needs to represent a `ref`. Source Edit

    proc invokeNewSeq(x: Any; len: int) {...}{.raises: [], tags: [].}

  performs `newSeq(x, len)`. `x` needs to represent a `seq`. Source Edit

    proc extendSeq(x: Any) {...}{.raises: [], tags: [].}

  performs `setLen(x, x.len+1)`. `x` needs to represent a `seq`. Source Edit

    proc setObjectRuntimeType(x: Any) {...}{.raises: [], tags: [].}

  this needs to be called to set `x`'s runtime object type field. Source Edit

    proc `[]`(x: Any; i: int): Any {...}{.raises: [IndexError, ValueError], tags: [].}

  accessor for an any `x` that represents an array or a sequence. Source Edit

    proc `[]=`(x: Any; i: int; y: Any) {...}{.raises: [IndexError, ValueError], tags: [].}

  accessor for an any `x` that represents an array or a sequence. Source Edit

    proc len(x: Any): int {...}{.raises: [], tags: [].}

  len for an any `x` that represents an array or a sequence. Source Edit

    proc base(x: Any): Any {...}{.raises: [], tags: [].}

  returns base Any (useful for inherited object types). Source Edit

    proc isNil(x: Any): bool {...}{.raises: [], tags: [].}

  `isNil` for an any `x` that represents a cstring, proc or some pointer type. Source Edit

    proc getPointer(x: Any): pointer {...}{.raises: [], tags: [].}

  retrieve the pointer value out of `x`. `x` needs to be of kind `akString`, `akCString`, `akProc`, `akRef`, `akPtr`, `akPointer`, `akSequence`. Source Edit

    proc setPointer(x: Any; y: pointer) {...}{.raises: [], tags: [].}

  sets the pointer value of `x`. `x` needs to be of kind `akString`, `akCString`, `akProc`, `akRef`, `akPtr`, `akPointer`, `akSequence`. Source Edit

    proc `[]=`(x: Any; fieldName: string; value: Any) {...}{.raises: [ValueError], tags: [].}

  sets a field of `x`; `x` represents an object or a tuple. Source Edit

    proc `[]`(x: Any; fieldName: string): Any {...}{.raises: [ValueError], tags: [].}

  gets a field of `x`; `x` represents an object or a tuple. Source Edit

    proc `[]`(x: Any): Any {...}{.raises: [], tags: [].}

  dereference operation for the any `x` that represents a ptr or a ref. Source Edit

    proc `[]=`(x, y: Any) {...}{.raises: [], tags: [].}

  dereference operation for the any `x` that represents a ptr or a ref. Source Edit

    proc getInt(x: Any): int {...}{.raises: [], tags: [].}

  retrieve the int value out of `x`. `x` needs to represent an int. Source Edit

    proc getInt8(x: Any): int8 {...}{.raises: [], tags: [].}

  retrieve the int8 value out of `x`. `x` needs to represent an int8. Source Edit

    proc getInt16(x: Any): int16 {...}{.raises: [], tags: [].}

  retrieve the int16 value out of `x`. `x` needs to represent an int16. Source Edit

    proc getInt32(x: Any): int32 {...}{.raises: [], tags: [].}

  retrieve the int32 value out of `x`. `x` needs to represent an int32. Source Edit

    proc getInt64(x: Any): int64 {...}{.raises: [], tags: [].}

  retrieve the int64 value out of `x`. `x` needs to represent an int64. Source Edit

    proc getBiggestInt(x: Any): BiggestInt {...}{.raises: [], tags: [].}

  retrieve the integer value out of `x`. `x` needs to represent some integer, a bool, a char, an enum or a small enough bit set. The value might be sign-extended to `BiggestInt`. Source Edit

    proc setBiggestInt(x: Any; y: BiggestInt) {...}{.raises: [], tags: [].}

  sets the integer value of `x`. `x` needs to represent some integer, a bool, a char, an enum or a small enough bit set. Source Edit

    proc getUInt(x: Any): uint {...}{.raises: [], tags: [].}

  retrieve the uint value out of `x`, `x` needs to represent an uint. Source Edit

    proc getUInt8(x: Any): uint8 {...}{.raises: [], tags: [].}

  retrieve the uint8 value out of `x`, `x` needs to represent an uint8. Source Edit

    proc getUInt16(x: Any): uint16 {...}{.raises: [], tags: [].}

  retrieve the uint16 value out of `x`, `x` needs to represent an uint16. Source Edit

    proc getUInt32(x: Any): uint32 {...}{.raises: [], tags: [].}

  retrieve the uint32 value out of `x`, `x` needs to represent an uint32. Source Edit

    proc getUInt64(x: Any): uint64 {...}{.raises: [], tags: [].}

  retrieve the uint64 value out of `x`, `x` needs to represent an uint64. Source Edit

    proc getBiggestUint(x: Any): uint64 {...}{.raises: [], tags: [].}

  retrieve the unsigned integer value out of `x`. `x` needs to represent an unsigned integer. Source Edit

    proc setBiggestUint(x: Any; y: uint64) {...}{.raises: [], tags: [].}

  sets the unsigned integer value of `c`. `c` needs to represent an unsigned integer. Source Edit

    proc getChar(x: Any): char {...}{.raises: [], tags: [].}

  retrieve the char value out of `x`. `x` needs to represent a char. Source Edit

    proc getBool(x: Any): bool {...}{.raises: [], tags: [].}

  retrieve the bool value out of `x`. `x` needs to represent a bool. Source Edit

    proc skipRange(x: Any): Any {...}{.raises: [], tags: [].}

  skips the range information of `x`. Source Edit

    proc getEnumOrdinal(x: Any; name: string): int {...}{.raises: [], tags: [].}

  gets the enum field ordinal from `name`. `x` needs to represent an enum but is only used to access the type information. In case of an error `low(int)` is returned. Source Edit

    proc getEnumField(x: Any; ordinalValue: int): string {...}{.raises: [], tags: [].}

  gets the enum field name as a string. `x` needs to represent an enum but is only used to access the type information. The field name of `ordinalValue` is returned. Source Edit

    proc getEnumField(x: Any): string {...}{.raises: [], tags: [].}

  gets the enum field name as a string. `x` needs to represent an enum. Source Edit

    proc getFloat(x: Any): float {...}{.raises: [], tags: [].}

  retrieve the float value out of `x`. `x` needs to represent an float. Source Edit

    proc getFloat32(x: Any): float32 {...}{.raises: [], tags: [].}

  retrieve the float32 value out of `x`. `x` needs to represent an float32. Source Edit

    proc getFloat64(x: Any): float64 {...}{.raises: [], tags: [].}

  retrieve the float64 value out of `x`. `x` needs to represent an float64. Source Edit

    proc getBiggestFloat(x: Any): BiggestFloat {...}{.raises: [], tags: [].}

  retrieve the float value out of `x`. `x` needs to represent some float. The value is extended to `BiggestFloat`. Source Edit

    proc setBiggestFloat(x: Any; y: BiggestFloat) {...}{.raises: [], tags: [].}

  sets the float value of `x`. `x` needs to represent some float. Source Edit

    proc getString(x: Any): string {...}{.raises: [], tags: [].}

  retrieve the string value out of `x`. `x` needs to represent a string. Source Edit

    proc setString(x: Any; y: string) {...}{.raises: [], tags: [].}

  sets the string value of `x`. `x` needs to represent a string. Source Edit

    proc getCString(x: Any): cstring {...}{.raises: [], tags: [].}

  retrieve the cstring value out of `x`. `x` needs to represent a cstring. Source Edit

    proc assign(x, y: Any) {...}{.raises: [], tags: [].}

  copies the value of `y` to `x`. The assignment operator for `Any` does NOT do this; it performs a shallow copy instead! Source Edit

    proc inclSetElement(x: Any; elem: int) {...}{.raises: [], tags: [].}

  includes an element `elem` in `x`. `x` needs to represent a Nim bitset. Source Edit

# Iterators

    iterator fields(x: Any): tuple[name: string, any: Any] {...}{.raises: [], tags: [].}

  iterates over every active field of the any `x` that represents an object or a tuple. Source Edit

    iterator elements(x: Any): int {...}{.raises: [], tags: [].}

  iterates over every element of `x` that represents a Nim bitset. Source Edit