doc: `Compile-time reflection` section with *new features* (#5962)
parent
433610b5c0
commit
6a8a589adb
30
doc/docs.md
30
doc/docs.md
|
@ -79,7 +79,7 @@ you can do in V.
|
||||||
* [Debugging generated C code](#debugging-generated-c-code)
|
* [Debugging generated C code](#debugging-generated-c-code)
|
||||||
* [Conditional compilation](#conditional-compilation)
|
* [Conditional compilation](#conditional-compilation)
|
||||||
* [Compile time pseudo variables](#compile-time-pseudo-variables)
|
* [Compile time pseudo variables](#compile-time-pseudo-variables)
|
||||||
* [Reflection via codegen](#reflection-via-codegen)
|
* [Compile-time reflection](#compile-time-reflection)
|
||||||
* [Limited operator overloading](#limited-operator-overloading)
|
* [Limited operator overloading](#limited-operator-overloading)
|
||||||
* [Inline assembly](#inline-assembly)
|
* [Inline assembly](#inline-assembly)
|
||||||
* [Translating C/C++ to V](#translating-cc-to-v)
|
* [Translating C/C++ to V](#translating-cc-to-v)
|
||||||
|
@ -2268,26 +2268,38 @@ that does nothing.
|
||||||
`if _unlikely_(bool expression) {` similar to `_likely_(x)`, but it hints that
|
`if _unlikely_(bool expression) {` similar to `_likely_(x)`, but it hints that
|
||||||
the boolean expression is highly improbable. In the JS backend, that does nothing.
|
the boolean expression is highly improbable. In the JS backend, that does nothing.
|
||||||
|
|
||||||
## Reflection via codegen
|
<a id='Reflection via codegen'>
|
||||||
|
|
||||||
|
## Compile-time reflection
|
||||||
|
|
||||||
Having built-in JSON support is nice, but V also allows you to create efficient
|
Having built-in JSON support is nice, but V also allows you to create efficient
|
||||||
serializers for any data format:
|
serializers for any data format. V has compile-time `if` and `for` constructs:
|
||||||
|
|
||||||
```v
|
```v
|
||||||
// TODO: not implemented yet
|
// TODO: not implemented yet
|
||||||
|
|
||||||
|
struct User {
|
||||||
|
name string
|
||||||
|
age int
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note: T should be passed a struct name only
|
||||||
fn decode<T>(data string) T {
|
fn decode<T>(data string) T {
|
||||||
mut result := T{}
|
mut result := T{}
|
||||||
for field in T.fields {
|
// compile-time `for` loop
|
||||||
if field.typ == 'string' {
|
// T.fields gives an array of a field metadata type
|
||||||
result.$field = get_string(data, field.name)
|
$for field in T.fields {
|
||||||
} else if field.typ == 'int' {
|
$if field.Type is string {
|
||||||
result.$field = get_int(data, field.name)
|
// $(string_expr) produces an identifier
|
||||||
|
result.$(field.name) = get_string(data, field.name)
|
||||||
|
} else $if field.Type is int {
|
||||||
|
result.$(field.name) = get_int(data, field.name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// generates to:
|
// `decode<User>` generates:
|
||||||
fn decode_User(data string) User {
|
fn decode_User(data string) User {
|
||||||
mut result := User{}
|
mut result := User{}
|
||||||
result.name = get_string(data, 'name')
|
result.name = get_string(data, 'name')
|
||||||
|
|
Loading…
Reference in New Issue