Acton
Tolk standard library

reflection

reflection.tolk Tolk standard library file

Definitions

reflect

struct reflect

reflect is a built-in struct, it has only static methods. Example: reflect.typeNameOf<int>() and other methods.

Source code

reflect.typeNameOf

@pure
fun reflect.typeNameOf<T>(): string

Get human-readable type name of any T for logging or low-level purposes. Example:

debug.printString(reflect.typeNameOf<int>());  // "int"
Source code

reflect.typeNameOfObject

@pure
fun reflect.typeNameOfObject<T>(anyVariable: T): string

Get human-readable type name of any object for logging or low-level purposes. Example:

debug.printString(reflect.typeNameOfObject(42));   // "int"
Source code

reflect.typeAbiJsonOf

@pure
fun reflect.typeAbiJsonOf<T>(): string

Get ABI-JSON type description of any T for compile-time reflection.

Source code

reflect.typeAbiJsonOfObject

@pure
fun reflect.typeAbiJsonOfObject<T>(anyVariable: T): string

Get ABI-JSON type description of any object for compile-time reflection.

Source code

reflect.stackSizeOf

@pure
fun reflect.stackSizeOf<T>(): int

Returns the number of stack slots a type T occupies. Example:

reflect.stackSizeOf<int>()     // 1
reflect.stackSizeOf<(int, int)>()  // 2
Source code

reflect.stackSizeOfObject

@pure
fun reflect.stackSizeOfObject<T>(anyVariable: T): int

Returns the number of stack slots any object occupies.

  • for nullableInt it's 1, because int? is 1 TVM slot holding either NULL or a value.
  • for somePoint it's 2 for struct Point { x:int, y:int }: two fields one slot per each. Useful for debugging or when preparing stack contents for RUNVM.
Source code

reflect.serializationPrefixOf

@pure
fun reflect.serializationPrefixOf<T>(): (int, int)

Returns serialization prefix of a struct. Example: for struct (0xF0) AssetRegular { ... } will return (240, 8).

reflect.serializationPrefixOf<AssetRegular>().1   // 8
Source code

reflect.estimateSerializationOf

@pure
fun reflect.estimateSerializationOf<T>(): (int, int, int, int)

Estimate serialization size of any type. Returns (minBits, maxBits, minRefs, maxRefs). Example:

reflect.estimateSerializationOf<int8?>()     // (1 9 0 0)
Source code

SourceLocation

struct SourceLocation {
    lineNo: int
    charNo: int
    fileName: string
}

Source location information, resolved at compile time. See reflect.sourceLocation for examples.

Source code

reflect.sourceLocation

@pure
fun reflect.sourceLocation(): SourceLocation

Returns the source location where this function is called. Can be used to retrieve current file/line, similar to FILE in some languages. Example:

var curLineNo = reflect.sourceLocation().lineNo

When used as a default parameter value, captures the caller's location. Example:

fun log(msg: string, loc: SourceLocation = reflect.sourceLocation()) {
    debug.print(loc.lineNo);
}

fun demo() {
    log("a");    // prints K — current line no
    log("b");    // prints K+1
}
Source code

reflect.sourceLocationAsString

@pure
fun reflect.sourceLocationAsString(): string

Behaves like reflect.sourceLocation, but returns a string "fileName:lineNo:charNo". fileName contains the full (real) path of the source file. Being used as a default parameter value, captures the caller's location.

Source code

Last updated on

On this page