reflection
reflection.tolk Tolk standard library file
Definitions
reflect
struct reflectreflect is a built-in struct, it has only static methods.
Example: reflect.typeNameOf<int>() and other methods.
reflect.typeNameOf
@pure
fun reflect.typeNameOf<T>(): stringGet human-readable type name of any T for logging or low-level purposes. Example:
debug.printString(reflect.typeNameOf<int>()); // "int"reflect.typeNameOfObject
@pure
fun reflect.typeNameOfObject<T>(anyVariable: T): stringGet human-readable type name of any object for logging or low-level purposes. Example:
debug.printString(reflect.typeNameOfObject(42)); // "int"reflect.typeAbiJsonOf
@pure
fun reflect.typeAbiJsonOf<T>(): stringGet ABI-JSON type description of any T for compile-time reflection.
Source codereflect.typeAbiJsonOfObject
@pure
fun reflect.typeAbiJsonOfObject<T>(anyVariable: T): stringGet ABI-JSON type description of any object for compile-time reflection.
Source codereflect.stackSizeOf
@pure
fun reflect.stackSizeOf<T>(): intReturns the number of stack slots a type T occupies. Example:
reflect.stackSizeOf<int>() // 1
reflect.stackSizeOf<(int, int)>() // 2reflect.stackSizeOfObject
@pure
fun reflect.stackSizeOfObject<T>(anyVariable: T): intReturns 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.
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 // 8reflect.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)SourceLocation
struct SourceLocation {
lineNo: int
charNo: int
fileName: string
}Source location information, resolved at compile time. See reflect.sourceLocation for examples.
Source codereflect.sourceLocation
@pure
fun reflect.sourceLocation(): SourceLocationReturns 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().lineNoWhen 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
}reflect.sourceLocationAsString
@pure
fun reflect.sourceLocationAsString(): stringBehaves 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.
Last updated on