From 2e9f8e6633fd69b20ce2ad6ff9296ade6ae73434 Mon Sep 17 00:00:00 2001 From: phoebe Date: Mon, 3 Jul 2023 22:19:08 +0200 Subject: [PATCH] docs: add compile time types (#18761) --- doc/docs.md | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/doc/docs.md b/doc/docs.md index dfd119abb0..df369abdc0 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -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.