Overview
The stubs
module contains files describing some features of the V language that are not explicitly
described in the standard library.
For example, some compile-time functions or attributes.
This is not real code, it is only needed for Doki and IDE to be able to show documentation and jump to definitions.
Aliases
type u64 = u64
u64 is the set of all unsigned 64-bit integers.
Range: 0 through 18446744073709551615.
type i64 = i64
i64 is the set of all signed 64-bit integers.
Range: -9223372036854775808 through 9223372036854775807.
type byte = u8
byte is an alias for u8 and is equivalent to u8 in all ways. It is used, by convention, to distinguish byte values from 8-bit unsigned integer values.
type rune = int
rune is an alias for int and is equivalent to int in all ways. It is used, by convention, to distinguish character values from integer values.
type char = u8
char is an alias for u8 and is equivalent to u8 in all ways.
Mostly used for C interoperability.
type voidptr = voidptr
voidptr is an untyped pointer.
Mostly used for C interoperability.
type byteptr = byteptr
byteptr is a byte pointer.
Mostly used for C interoperability.
type charptr = charptr
charptr is a char pointer.
Mostly used for C interoperability.
Constants
pub const @METHOD = ''
@METHOD replaced with name of the current V method and receiver type: ReceiverType.MethodName
.
pub const @STRUCT = ''
@STRUCT replaced with the name of the current V struct.
pub const @FILE = ''
@FILE replaced with the absolute path of the V source file.
pub const @LINE = ''
@LINE replaced with the V line number where it appears (as a string).
pub const @FILE_LINE = ''
@FILE_LINE replaced with @FILE:@LINE
, but the file part is a relative path.
pub const @COLUMN = ''
@COLUMN replaced with the column where it appears (as a string).
pub const @VEXEROOT = ''
@VEXEROOT replaced with the folder, where the V executable is.
pub const @VHASH = ''
@VHASH replaced with the shortened commit hash of the V compiler.
pub const @VMOD_FILE = ''
@VMOD_FILE replaced with the contents of the nearest v.mod file.
pub const @VMODROOT = ''
@VMODROOT replaced with the folder, where the nearest v.mod file is.
pub const windows = false
windows set to true
if the current OS is Windows.
pub const freebsd = false
freebsd set to true
if the current OS is FreeBSD.
pub const openbsd = false
openbsd set to true
if the current OS is OpenBSD.
pub const serenity = false
serenity set to true
if the current OS is Serenity.
pub const android = false
android set to true
if the current OS is Android.
pub const emscripten = false
emscripten set to true
if the current OS is Emscripten.
pub const js_node = false
js_node set to true
if the current platform is Node.js.
pub const js_freestanding = false
js_freestanding set to true
if the current platform is pure JavaScript.
pub const js_browser = false
js_browser set to true
if the current platform is JavaScript in a browser.
pub const dragonfly = false
dragonfly set to true
if the current OS is Dragonfly.
pub const solaris = false
solaris set to true
if the current OS is Solaris.
pub const cplusplus = false
cpp set to true
if the current compiler is C++.
pub const little_endian = false
little_endian set to true
if the current platform is little endian.
pub const big_endian = false
big_endian set to true
if the current platform is big endian.
pub const debug = false
debug set to true
if the -g flag is passed to the compiler.
pub const prod = false
prod set to true
if the -prod flag is passed to the compiler.
pub const glibc = false
glibc set to true
if the -glibc flag is passed to the compiler.
pub const prealloc = false
prealloc set to true
if the -prealloc flag is passed to the compiler.
pub const no_bounds_checking = false
no_bounds_checking set to true
if the -no_bounds_checking flag is passed to the compiler.
pub const freestanding = false
freestanding set to true
if the -freestanding flag is passed to the compiler.
pub const no_segfault_handler = false
no_segfault_handler set to true
if the -no_segfault_handler flag is passed to the compiler.
pub const no_backtrace = false
no_backtrace set to true
if the -no_backtrace flag is passed to the compiler.
pub const no_main = false
no_main set to true
if the -no_main flag is passed to the compiler.
pub const $int = TypeInfo{}
$int describes any integer type.
Example:
$for f in Test.fields { $if f.typ is $int { println(f.name) } }
pub const $float = TypeInfo{}
$float describes any float type.
Example:
$for f in Test.fields { $if f.typ is $float { println(f.name) } }
pub const $array = TypeInfo{}
$array describes any array type.
Example:
$for f in Test.fields { $if f.typ is $array { println(f.name) } }
pub const $map = TypeInfo{}
$map describes any map type.
Example:
$for f in Test.fields { $if f.typ is $map { println(f.name) } }
pub const $struct = TypeInfo{}
$struct describes any struct type.
Example:
$for f in Test.fields { $if f.typ is $struct { println(f.name) } }
pub const $interface = TypeInfo{}
$interface describes any interface type.
Example:
$for f in Test.fields { $if f.typ is $interface { println(f.name) } }
pub const $enum = TypeInfo{}
$enum describes any enum type.
Example:
$for f in Test.fields { $if f.typ is $enum { println(f.name) } }
pub const $alias = TypeInfo{}
$alias describes any alias type.
Example:
$for f in Test.fields { $if f.typ is $alias { println(f.name) } }
pub const $sumtype = TypeInfo{}
$sumtype describes any sumtype type.
Example:
$for f in Test.fields { $if f.typ is $sumtype { println(f.name) } }
pub const $function = TypeInfo{}
$function describes any function type.
Example:
$for f in Test.fields { $if f.typ is $function { println(f.name) } }
pub const $option = TypeInfo{}
$option describes any option type.
Example:
$for f in Test.fields { $if f.typ is $option { println(f.name) } }
pub const err = IError{}
err is a special variable that is set with an error and is used to handle errors in V.
It can be used inside two places:
- inside
or
block:
fn foo() !int { return error("not implemented"); } foo() or { panic(err); // ^^^ err is set with error("not implemented") }
- inside else block for if guard:
fn foo() !int { return error("not implemented"); } if val := foo() { // val is set with int } else { panic(err); // ^^^ err is set with error("not implemented") }
See Documentation for more details.
pub const $vweb = VWebTemplate{}
$vweb constant allows you to render HTML template in endpoint functions.
$vweb.html()
in method like <folder>_<name>() vweb.Result
render the <name>.html
in folder ./templates/<folder>
$vweb.html()
compiles an HTML template into V during compilation, and embeds the resulting code into the current function.
That means that the template automatically has access to that function's entire environment (like variables).
See vweb documentation for more information.
Example:
['/'] pub fn (mut app App) page_home() vweb.Result { // will render `./templates/page/home.html` return $vweb.html() }
Sum types
This section is empty.
Functions
This section is empty.
Structs
pub struct ArrayInit {
// index represent the current element index that is being initialized inside `init`.
//
// See [ArrayInit](#ArrayInit) documentation for more info.
index int
pub:
// len field represent number of pre-allocated and initialized elements in the array
//
// See [ArrayInit](#ArrayInit) documentation for more info.
len int
// cap field represent amount of memory space which has been reserved for elements,
// but not initialized or counted as elements
//
// See [ArrayInit](#ArrayInit) documentation for more info.
cap int
// init field represent default initializer for each element.
//
// See [ArrayInit](#ArrayInit) documentation for more info.
init element_type
}
Example:
arr := []int{} arr_with_len := []int{len: 1} // [0] arr_with_cap := []int{len: 1, cap: 100} // [0] arr_with_len_init := []int{len: 1, init: 1} [1] arr_with_init := []int{len: 2, cap: 100, init: index * 2} [0, 2]
Array initializer can contain three optional fields:
len
– length – number of pre-allocated and initialized elements in the array 2.cap
– capacity – amount of memory space which has been reserved for elements, but not initialized or counted as elements 3.init
– default initializer for each element
All three fields can be used independently of the others.
cap field
If cap
is not specified, it is set to len
. cap
cannot be smaller than len
.
cap
can be used for improving performance of array operations, since no reallocation will be needed.
arr := []int{len: 2} arr << 100 // there will be a reallocation that will slow down the program a bit arr_with_cap := []int{len: 2, cap: 10} arr_with_cap << 100 // no reallocation
init field
If init
is not specified, it is set to 0
for numerical type, ''
for string, etc.
arr := []int{len: 2} assert arr == [0, 0]
In init
field, you can use special index
variable to refer to the current index.
arr := []int{len: 3, init: index * 2} assert arr == [0, 2, 4]
pub struct TypeInfo {
pub mut:
idx int // index of the type in the type table
name string // name of the type
}
pub struct UnknownCDeclaration {
pub mut:
unknown_field &UnknownCDeclaration
}
pub struct ChanInit {
pub:
// cap fields describes the size of the buffered channel.
//
// The channel size describes the number of elements that can be
// written to the channel without blocking.
// If more elements are written to the channel than the buffer size,
// then the write is blocked until another thread reads the element
// from the channel and there is free space.
//
// If `cap == 0` (default), then the channel is not buffered.
//
// **Example**
// ```
// ch := chan int{cap: 10} // buffered channel
// ch <- 1 // no blocking
// ```
//
// **Example**
// ```
// ch := chan int{} // unbuffered channel
// ch <- 1 // block until another thread reads from the channel
// ```
cap int
}
Interfaces
This section is empty.
Enums
pub enum CompressionType {
zlib
}
CompressionType is the type of compression used for the embedded file.
See [$embed_file] for more details.
pub enum FlagEnum {}
has checks if the enum value has the passed flag.
Example:
[flag] enum Permissions { read // = 0b0001 write // = 0b0010 other // = 0b0100 } fn main() { p := Permissions.read assert p.has(.read) // test if p has read flag assert p.has(.read | .other) // test if *at least one* of the flags is set }
all checks if the enum value has all passed flags.
Example:
[flag] enum Permissions { read // = 0b0001 write // = 0b0010 other // = 0b0100 } fn main() { p := Permissions.read | .write assert p.all(.read | .write) // test if *all* of the flags is set }
set sets the passed flags.
If the flag is already set, it will be ignored.
Example:
[flag] enum Permissions { read // = 0b0001 write // = 0b0010 other // = 0b0100 } fn main() { mut p := Permissions.read p.set(.write) assert p.has(.write) }
toggle toggles the passed flags.
If the flag is already set, it will be unset.
If the flag is not set, it will be set.
Example:
[flag] enum Permissions { read // = 0b0001 write // = 0b0010 other // = 0b0100 } fn main() { mut p := Permissions.read p.toggle(.read) assert !p.has(.read) }
clear clears the passed flags.
If the flag is not set, it will be ignored.
Example:
[flag] enum Permissions { read // = 0b0001 write // = 0b0010 other // = 0b0100 } fn main() { mut p := Permissions.read p.clear(.read) assert !p.has(.read) }
bool is the set of boolean values, true and false.