1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

docs: add compile time types (#18761)

This commit is contained in:
phoebe 2023-07-03 22:19:08 +02:00 committed by GitHub
parent 3f5995ace8
commit 2e9f8e6633
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -169,8 +169,9 @@ by using any of the following commands in a terminal:
* [Attributes](#attributes)
* [Conditional compilation](#conditional-compilation)
* [Compile time pseudo variables](#compile-time-pseudo-variables)
* [Compile-time reflection](#compile-time-reflection)
* [Compile time reflection](#compile-time-reflection)
* [Compile time code](#compile-time-code)
* [Compile time types](#compile-time-types)
* [Environment specific files](#environment-specific-files)
* [Memory-unsafe code](#memory-unsafe-code)
* [Structs with reference fields](#structs-with-reference-fields)
@ -2957,7 +2958,7 @@ const (
g: 0
b: 0
}
// evaluate function call at compile-time*
// evaluate function call at compile time*
blue = rgb(0, 0, 255)
)
@ -5503,10 +5504,12 @@ vm := vmod.decode( @VMOD_FILE ) or { panic(err) }
eprintln('${vm.name} ${vm.version}\n ${vm.description}')
```
### Compile-time reflection
### Compile time reflection
`$` is used as a prefix for compile time (also referred to as 'comptime') operations.
Having built-in JSON support is nice, but V also allows you to create efficient
serializers for any data format. V has compile-time `if` and `for` constructs:
serializers for any data format. V has compile time `if` and `for` constructs:
```v
struct User {
@ -5531,8 +5534,6 @@ for a more complete example.
### Compile time code
`$` is used as a prefix for compile-time operations.
#### `$if` condition
```v
@ -5719,6 +5720,27 @@ x.v:4:5: error: Linux is not supported
6 |
```
### Compile time types
Compile time types group multiple types into a general higher-level type. This is useful in
functions with generic parameters, where the input type must have a specific property, for example
the `.len` attribute in arrays.
V supports the following compile time types:
- `$alias` => matches [Type aliases](#type-aliases).
- `$array` => matches [Arrays](#arrays) and [Fixed Size Arrays](#fixed-size-arrays).
- `$enum` => matches [Enums](#enums).
- `$float` => matches `f32`, `f64` and float literals.
- `$function` => matches [Function Types](#function-types).
- `$int` => matches `int`, `i8`, `i16`, `i32`, `i64`, `u8`, `u16`, `u32`, `u64`, `isize`, `usize`
and integer literals.
- `$interface` => matches [Interfaces](#interfaces).
- `$map` => matches [Maps](#maps).
- `$option` => matches [Option Types](#optionresult-types-and-error-handling).
- `$struct` => matches [Structs](#structs).
- `$sumtype` => matches [Sum Types](#sum-types).
### Environment specific files
If a file has an environment-specific suffix, it will only be compiled for that environment.