Source Edit

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

Note: Even though Any and its operations hide the nasty low level details from its users, 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 module 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.

Example:

  1. import std/typeinfo
  2. var x: Any
  3. var i = 42
  4. x = i.toAny
  5. assert x.kind == akInt
  6. assert x.getInt == 42
  7. var s = @[1, 2, 3]
  8. x = s.toAny
  9. assert x.kind == akSequence
  10. assert x.len == 3

Imports

strimpl

Types

  1. Any = object
  2. when defined(js):
  3. else:

A type that can represent any nim value.

Danger: 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

  1. AnyKind = enum
  2. akNone = 0, ## invalid
  3. akBool = 1, ## bool
  4. akChar = 2, ## char
  5. akEnum = 14, ## enum
  6. akArray = 16, ## array
  7. akObject = 17, ## object
  8. akTuple = 18, ## tuple
  9. akSet = 19, ## set
  10. akRange = 20, ## range
  11. akPtr = 21, ## ptr
  12. akRef = 22, ## ref
  13. akSequence = 24, ## sequence
  14. akProc = 25, ## proc
  15. akPointer = 26, ## pointer
  16. akString = 28, ## string
  17. akCString = 29, ## cstring
  18. akInt = 31, ## int
  19. akInt8 = 32, ## int8
  20. akInt16 = 33, ## int16
  21. akInt32 = 34, ## int32
  22. akInt64 = 35, ## int64
  23. akFloat = 36, ## float
  24. akFloat32 = 37, ## float32
  25. akFloat64 = 38, ## float64
  26. akFloat128 = 39, ## float128
  27. akUInt = 40, ## uint
  28. akUInt8 = 41, ## uint8
  29. akUInt16 = 42, ## uin16
  30. akUInt32 = 43, ## uint32
  31. akUInt64 = 44 ## uint64

The kind of Any. Source Edit

Procs

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

Dereference operator for Any. x needs to represent a ptr or a ref. Source Edit

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

Gets a field of x. x needs to represent an object or a tuple. Source Edit

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

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

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

Dereference operator for Any. x needs to represent a ptr or a ref. Source Edit

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

Sets a field of x. x needs to represent an object or a tuple. Source Edit

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

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

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

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

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

Returns the base type of x (useful for inherited object types). Source Edit

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

Gets the base type’s kind. If x has no base type, akNone is returned. Source Edit

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

Returns the size of x’s base type. If x has no base type, 0 is returned. Source Edit

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

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

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

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

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

Retrieves 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Retrieves the float value out of x. x needs to represent a float. Source Edit

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

Retrieves the float32 value out of x. x needs to represent a float32. Source Edit

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

Retrieves the float64 value out of x. x needs to represent a float64. Source Edit

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Retrieves the uint value out of x. x needs to represent a uint. Source Edit

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

Retrieves the uint8 value out of x. x needs to represent a uint8. Source Edit

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

Retrieves the uint16 value out of x. x needs to represent a uint16. Source Edit

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

Retrieves the uint32 value out of x. x needs to represent a uint32. Source Edit

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

Retrieves the uint64 value out of x. x needs to represent a uint64. Source Edit

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

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

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

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

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

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

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

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

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

Gets the type kind. Source Edit

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

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

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

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

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

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

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

Sets the unsigned integer value of x. x needs to represent an unsigned integer. Source Edit

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

This needs to be called to set x’s runtime object type field. Source Edit

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

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

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

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

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

Returns the size of x’s type. Source Edit

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

Skips the range information of x. Source Edit

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

Constructs an Any object from x. This captures x’s address, so x can be modified with its Any wrapper! The caller needs to ensure that the wrapper does not live longer than x! Source Edit

Iterators

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

Iterates over every element of x. x needs to represent a set. Source Edit

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

Iterates over every active field of x. x needs to represent an object or a tuple. Source Edit