Functions available in this module:
- reflection.(*)_of_type[foo]() // Extract details from type or struct
- reflection.(*)_of(bar) // Extract details from type instance
(*) can be `methods`, `fields` or `attributes`.
Each function returns a map[string]T
where T
can be FunctionData
, FieldData
or StructAttribute
respectively.
- More details about these types can be found in
builtin.v
file.
fn fields_of[T](object T) map[string]FieldData
fn fields_of_type[T]() map[string]FieldData
fn attributes_of[T](object T) map[string]StructAttribute
fn attributes_of_type[T]() map[string]StructAttribute
fn methods_of[T](object T) map[string]FunctionData
fn methods_of_type[T]() map[string]FunctionData
import reflection { methods_of_type }
struct Dummy {}
[example: 'Lorem Ipsum']
fn (s Dummy) foo() {
// ...
}
fn main() {
methods := methods_of_type[Dummy]()
assert 'foo' in methods
dump(methods['foo'].attrs)
dump(methods['foo'])
}
[main.v:15] methods['foo'].attrs: ['example: Lorem Ipsum']
[main.v:16] methods['foo']: FunctionData{
name: 'foo'
attrs: ['example: Lorem Ipsum']
args: []
return_type: 1
typ: 0
}
.
├── main.v
└── reflection/
└── reflection.v
<>
was replaced to[]
(Sudden syntax change in generics)