Builtin functions
(e)print(ln)
: Printing to the console
println
is a simple yet powerful builtin function, that can print anything:
strings, numbers, arrays, maps, structs.
print
is the same as println
, but doesn't add a newline character at the end.
eprintln
is the same as println
, but prints to the standard error output.
eprint
is the same as eprintln
, but doesn't add a newline character at the end.
See also Printing Custom Types.
sizeof
: Getting the size of a type
sizeof
returns the size of a type in bytes.
This builtin function can be used in two ways:
sizeof[type]()
– returns the size of the type in bytessizeof(expr)
– returns the size of the type of the expression in bytes
typeof
: Getting the type of expression
See
typeof
description
in the Compile Time Reflection section.
isreftype
: Checking if a type is a reference type
isreftype
returns true
if the type is a reference type, false
otherwise.
__offsetof
: Getting the offset of a struct field
__offsetof
returns the offset of a struct field in bytes.
dump
: Dumping expressions at runtime
You can dump/trace the value of any V expression using dump(expr)
.
This function takes an expression and returns it, so you can use it anywhere in the expression.
dump(expr)
keeps track of the original location, the expression itself, and the value of the
expression.
Output:
[factorial.v:2] n <= 1: false
[factorial.v:2] n <= 1: false
[factorial.v:2] n <= 1: false
[factorial.v:2] n <= 1: false
[factorial.v:2] n <= 1: true
[factorial.v:3] 1: 1
[factorial.v:5] n * factorial(n - 1): 2
[factorial.v:5] n * factorial(n - 1): 6
[factorial.v:5] n * factorial(n - 1): 24
[factorial.v:5] n * factorial(n - 1): 120
120