Compile-time reflection
V provides compile time reflection to get information about data types and structures at compile time. With its help, you can, for example, make your own efficient serializer for any data format that will be generated during compilation.
typeof
: Getting the type of expression
typeof
returns the type of expression in TypeInfo
struct.
This builtin function can be used in two ways:
typeof[type]()
– returns the type information of the typetypeof(expr)
– returns the type information of the expression type
TypeInfo
struct definition:
When compiled, V assigns a unique index to each type in the program, so you can match types by comparing their indexes:
Unlike $if user.name is string
, which will be discussed later, this check can express conditions
more flexibly.
Although the compiler will replace
typeof(user.name).idx
with a constant, the conditions will still be checked at runtime, unlike$if user.name is string
, which will be checked at compile-time (and removed from the binary if the condition is always false).
Fields
Each
structure,
union
and
interface
has a fields
field that contains information about the fields of the structure:
The fields
field is of type []FieldData
.
You can see what fields this type has in
Standard Library API documentation.
For example, FieldData
has a typ
field that contains the type of the field.
While processing fields, you can separate logic by field name:
or by field type:
Field access
While processing the fields of a structure, you can access the fields of
an object of that structure using the syntax object.$(field.name)
:
You can also overwrite object field values:
Accessing object fields via
object.$(field.name)
only works inside the$for field in T.fields
loop.
Methods
Each type has a methods
field, which contains information about the methods of the type.
This field is of type []FunctionData
.
You can see what fields this type has in
Standard Library API documentation.
Method call
While processing the methods of a type, you can call the methods of
an object of that type using the syntax object.$method(args)
:
Attributes
Each type has an attributes
field, which contains information about the attributes of the type.
This field is of type []StructAttribute
.
You can see what fields this type has in
Standard Library API documentation.
Currently, function attributes cannot be retrieved.
Enums values
Each enum has a values
field, which contains information about the values of the enum.
The values
field is of type []EnumData
.
You can see what fields this type has in
Standard Library API documentation.
Type checking
The types stored in the typ
field in FieldData
or FunctionData
can be compared using the is
operator:
!is
is also supported:
As with normal if
expressions, you can use the in
statement:
Although
i32
is defined as atype alias i32 = int
, they are treated as separate types. Therefore, a field of typei32
will not match eitheris int
orin [int]
. The same holds true in reverse.
For the convenience of checks, V defines some type constants:
$int
- any integer type$float
- any floating point type$array
- any array$map
- any map$struct
- any structure$interface
- any interface$enum
– any enum$alias
- any alias type$sumtype
– any summary type$function
- any function
Example:
To check the type of field, you need to use
$if
rather thanif
.