2019-11-21 04:36:55 +03:00
# V Documentation
## Introduction
V is a statically typed compiled programming language designed for building maintainable software.
2020-08-16 20:16:59 +03:00
It's similar to Go and its design has also been influenced by Oberon, Rust, Swift,
Kotlin, and Python.
2019-11-21 04:36:55 +03:00
2020-12-17 14:28:29 +03:00
V is a very simple language. Going through this documentation will take you about an hour,
2020-05-07 13:30:41 +03:00
and by the end of it you will have pretty much learned the entire language.
2019-11-21 04:36:55 +03:00
2020-05-07 13:30:41 +03:00
The language promotes writing simple and clear code with minimal abstraction.
2020-05-05 16:42:53 +03:00
2020-10-01 23:41:17 +03:00
Despite being simple, V gives the developer a lot of power.
Anything you can do in other languages, you can do in V.
2019-11-21 04:36:55 +03:00
2021-01-03 08:38:03 +03:00
## Install from source
The major way to get the latest and greatest V, is to __install it from source__ .
It is __easy__ , and it usually takes __only a few seconds__ .
### Linux, macOS, FreeBSD, etc:
2021-02-07 13:53:26 +03:00
You need `git` , and a C compiler like `tcc` , `gcc` or `clang` , and `make` :
2021-01-03 08:38:03 +03:00
```bash
2021-02-28 19:01:31 +03:00
git clone https://github.com/vlang/v
cd v
2021-02-07 13:53:26 +03:00
make
2021-01-03 08:38:03 +03:00
```
### Windows:
2021-02-07 13:53:26 +03:00
You need `git` , and a C compiler like `tcc` , `gcc` , `clang` or `msvc` :
2021-01-03 08:38:03 +03:00
```bash
2021-01-05 13:57:12 +03:00
git clone https://github.com/vlang/v
2021-01-03 08:38:03 +03:00
cd v
2021-02-07 13:53:26 +03:00
make.bat -tcc
2021-01-03 08:38:03 +03:00
```
2021-02-07 13:53:26 +03:00
NB: You can also pass one of `-gcc` , `-msvc` , `-clang` to `make.bat` instead,
if you do prefer to use a different C compiler, but -tcc is small, fast, and
easy to install (V will download a prebuilt binary automatically).
2021-01-03 08:38:03 +03:00
2021-02-27 15:25:30 +03:00
It is recommended to add this folder to the PATH of your environment variables.
This can be done with the command `v.exe symlink` .
2021-01-08 23:37:02 +03:00
### Android
Running V graphical apps on Android is also possible via [vab ](https://github.com/vlang/vab ).
V Android dependencies: **V** , **Java JDK** >= 8, Android **SDK + NDK** .
1. Install dependencies (see [vab ](https://github.com/vlang/vab ))
2021-02-12 02:36:47 +03:00
2. Connect your Android device
2021-01-08 23:37:02 +03:00
3. Run:
```bash
git clone https://github.com/vlang/vab & & cd vab & & v vab.v
./vab --device auto run /path/to/v/examples/sokol/particles
```
For more details and troubleshooting, please visit the [vab GitHub repository ](https://github.com/vlang/vab ).
2020-05-10 16:49:43 +03:00
## Table of Contents
2020-05-11 21:21:41 +03:00
2020-05-10 16:49:43 +03:00
< table >
2020-06-26 21:01:30 +03:00
< tr > < td width = 33% valign = top >
2020-06-26 21:00:52 +03:00
* [Hello world ](#hello-world )
2021-03-04 22:34:16 +03:00
* [Running a project folder ](#running-a-project-folder-with-several-files )
2020-06-26 21:00:52 +03:00
* [Comments ](#comments )
* [Functions ](#functions )
2020-07-23 19:55:54 +03:00
* [Returning multiple values ](#returning-multiple-values )
2021-09-30 12:43:26 +03:00
* [Hoistings ](#hoistings )
2020-07-23 19:55:54 +03:00
* [Symbol visibility ](#symbol-visibility )
2020-06-26 21:00:52 +03:00
* [Variables ](#variables )
2021-06-22 12:39:57 +03:00
* [V types ](#v-types )
2020-06-26 21:00:52 +03:00
* [Strings ](#strings )
2020-07-14 20:26:14 +03:00
* [Numbers ](#numbers )
2020-06-26 21:00:52 +03:00
* [Arrays ](#arrays )
2021-02-02 20:51:40 +03:00
* [Fixed size arrays ](#fixed-size-arrays )
2020-06-26 21:00:52 +03:00
* [Maps ](#maps )
2020-07-23 19:55:54 +03:00
* [Module imports ](#module-imports )
* [Statements & expressions ](#statements--expressions )
2020-06-26 21:00:52 +03:00
* [If ](#if )
2020-07-23 19:55:54 +03:00
* [In operator ](#in-operator )
2020-06-26 21:00:52 +03:00
* [For loop ](#for-loop )
* [Match ](#match )
* [Defer ](#defer )
* [Structs ](#structs )
2020-07-23 19:55:54 +03:00
* [Embedded structs ](#embedded-structs )
* [Default field values ](#default-field-values )
2021-06-14 13:12:02 +03:00
* [Short struct literal syntax ](#short-struct-literal-syntax )
2020-06-26 21:00:52 +03:00
* [Access modifiers ](#access-modifiers )
* [Methods ](#methods )
2021-03-26 20:50:29 +03:00
* [Unions ](#unions )
2020-06-26 21:00:52 +03:00
2020-06-26 21:01:30 +03:00
< / td > < td width = 33% valign = top >
2020-06-26 21:00:52 +03:00
* [Functions 2 ](#functions-2 )
* [Pure functions by default ](#pure-functions-by-default )
2020-06-30 15:29:55 +03:00
* [Mutable arguments ](#mutable-arguments )
2021-02-24 21:27:48 +03:00
* [Variable number of arguments ](#variable-number-of-arguments )
2021-06-13 21:17:57 +03:00
* [Anonymous & higher-order functions ](#anonymous--higher-order-functions )
2021-09-30 12:43:26 +03:00
* [Closures ](#closures )
2020-06-26 21:00:52 +03:00
* [References ](#references )
* [Constants ](#constants )
2020-12-28 21:29:58 +03:00
* [Builtin functions ](#builtin-functions )
2021-02-13 17:57:51 +03:00
* [Printing custom types ](#printing-custom-types )
* [Modules ](#modules )
2021-06-22 12:39:57 +03:00
* [Type Declarations ](#type-declarations )
2020-06-26 21:00:52 +03:00
* [Interfaces ](#interfaces )
* [Enums ](#enums )
* [Sum types ](#sum-types )
2021-03-26 20:50:29 +03:00
* [Type aliases ](#type-aliases )
2020-06-26 21:00:52 +03:00
* [Option/Result types & error handling ](#optionresult-types-and-error-handling )
* [Generics ](#generics )
* [Concurrency ](#concurrency )
2021-01-16 00:40:26 +03:00
* [Spawning Concurrent Tasks ](#spawning-concurrent-tasks )
2020-09-20 17:20:56 +03:00
* [Channels ](#channels )
2021-01-16 00:40:26 +03:00
* [Shared Objects ](#shared-objects )
2020-06-26 21:00:52 +03:00
* [Decoding JSON ](#decoding-json )
* [Testing ](#testing )
* [Memory management ](#memory-management )
2021-07-16 21:40:51 +03:00
* [Stack and Heap ](#stack-and-heap )
2020-06-26 21:00:52 +03:00
* [ORM ](#orm )
2020-07-14 20:26:14 +03:00
< / td > < td valign = top >
2020-06-26 21:00:52 +03:00
* [Writing documentation ](#writing-documentation )
* [Tools ](#tools )
2020-12-27 14:36:00 +03:00
* [v fmt ](#v-fmt )
2020-06-26 21:00:52 +03:00
* [Profiling ](#profiling )
2021-10-31 12:32:31 +03:00
* [Package Management ](#package-management )
* [Publish package ](#publish-package )
2020-12-27 14:36:00 +03:00
* [Advanced Topics ](#advanced-topics )
2021-04-12 12:01:29 +03:00
* [Dumping expressions at runtime ](#dumping-expressions-at-runtime )
2020-07-23 01:36:23 +03:00
* [Memory-unsafe code ](#memory-unsafe-code )
2021-03-08 21:52:32 +03:00
* [Structs with reference fields ](#structs-with-reference-fields )
2021-02-24 21:27:48 +03:00
* [sizeof and __offsetof ](#sizeof-and-__offsetof )
2021-02-26 09:28:37 +03:00
* [Calling C from V ](#calling-c-from-v )
2021-07-31 02:31:15 +03:00
* [Calling V from C ](#calling-v-from-c )
2021-07-20 08:31:20 +03:00
* [Atomics ](#atomics )
2021-07-29 10:57:31 +03:00
* [Global Variables ](#global-variables )
2021-06-04 17:55:22 +03:00
* [Debugging ](#debugging )
2020-06-26 21:00:52 +03:00
* [Conditional compilation ](#conditional-compilation )
* [Compile time pseudo variables ](#compile-time-pseudo-variables )
2020-08-11 18:57:39 +03:00
* [Compile-time reflection ](#compile-time-reflection )
2020-06-26 21:00:52 +03:00
* [Limited operator overloading ](#limited-operator-overloading )
* [Inline assembly ](#inline-assembly )
2020-12-17 14:25:31 +03:00
* [Translating C to V ](#translating-c-to-v )
2020-06-26 21:00:52 +03:00
* [Hot code reloading ](#hot-code-reloading )
* [Cross compilation ](#cross-compilation )
* [Cross-platform shell scripts in V ](#cross-platform-shell-scripts-in-v )
* [Attributes ](#attributes )
2021-02-01 22:09:25 +03:00
* [Goto ](#goto )
2020-06-26 21:00:52 +03:00
* [Appendices ](#appendices )
* [Keywords ](#appendix-i-keywords )
* [Operators ](#appendix-ii-operators )
2020-06-26 20:36:40 +03:00
2020-06-26 21:12:52 +03:00
< / td > < / tr >
2020-05-10 16:49:43 +03:00
< / table >
2020-05-11 21:21:41 +03:00
2020-11-03 03:04:14 +03:00
<!--
2021-01-10 16:58:45 +03:00
NB: there are several special keywords, which you can put after the code fences for v:
2021-02-05 18:46:20 +03:00
compile, live, ignore, failcompile, oksyntax, badsyntax, wip, nofmt
For more details, do: `v check-md`
2020-11-03 03:04:14 +03:00
-->
2020-05-05 16:42:53 +03:00
2019-11-21 04:36:55 +03:00
## Hello World
2020-11-03 03:04:14 +03:00
2019-11-21 04:36:55 +03:00
```v
fn main() {
2020-12-06 00:54:41 +03:00
println('hello world')
2019-11-21 04:36:55 +03:00
}
```
2020-10-01 23:41:17 +03:00
Save this snippet into a file named `hello.v` . Now do: `v run hello.v` .
2019-11-21 04:36:55 +03:00
2020-07-09 18:14:14 +03:00
> That is assuming you have symlinked your V with `v symlink`, as described
2020-05-31 12:12:37 +03:00
[here ](https://github.com/vlang/v/blob/master/README.md#symlinking ).
2020-10-01 23:41:17 +03:00
If you haven't yet, you have to type the path to V manually.
2020-05-31 08:42:34 +03:00
2020-10-01 23:41:17 +03:00
Congratulations - you just wrote and executed your first V program!
2020-05-31 08:42:34 +03:00
2020-10-01 23:41:17 +03:00
You can compile a program without execution with `v hello.v` .
2020-05-31 12:12:37 +03:00
See `v help` for all supported commands.
2020-05-31 08:42:34 +03:00
2020-10-01 23:41:17 +03:00
From the example above, you can see that functions are declared with the `fn` keyword.
The return type is specified after the function name.
2020-12-22 18:19:55 +03:00
In this case `main` doesn't return anything, so there is no return type.
2019-11-21 04:36:55 +03:00
2020-12-22 18:19:55 +03:00
As in many other languages (such as C, Go, and Rust), `main` is the entry point of your program.
2019-11-21 04:36:55 +03:00
2020-10-01 23:41:17 +03:00
`println` is one of the few built-in functions.
It prints the value passed to it to standard output.
2019-11-21 04:36:55 +03:00
`fn main()` declaration can be skipped in one file programs.
2020-10-01 23:41:17 +03:00
This is useful when writing small programs, "scripts", or just learning the language.
For brevity, `fn main()` will be skipped in this tutorial.
2019-11-21 04:36:55 +03:00
2020-10-01 23:41:17 +03:00
This means that a "hello world" program in V is as simple as
2019-11-21 04:36:55 +03:00
```v
println('hello world')
```
2021-03-04 22:34:16 +03:00
## Running a project folder with several files
Suppose you have a folder with several .v files in it, where one of them
2021-03-09 19:24:21 +03:00
contains your `main()` function, and the other files have other helper
2021-03-04 23:00:07 +03:00
functions. They may be organized by topic, but still *not yet* structured
2021-03-04 22:52:55 +03:00
enough to be their own separate reusable modules, and you want to compile
2021-03-04 22:34:16 +03:00
them all into one program.
2021-03-04 22:52:55 +03:00
In other languages, you would have to use includes or a build system
2021-03-04 22:34:16 +03:00
to enumerate all files, compile them separately to object files,
then link them into one final executable.
In V however, you can compile and run the whole folder of .v files together,
2021-03-04 22:52:55 +03:00
using just `v run .` . Passing parameters also works, so you can
2021-03-04 22:34:16 +03:00
do: `v run . --yourparam some_other_stuff`
The above will first compile your files into a single program (named
after your folder/project), and then it will execute the program with
`--yourparam some_other_stuff` passed to it as CLI parameters.
Your program can then use the CLI parameters like this:
```v
import os
println(os.args)
```
2021-03-04 22:52:55 +03:00
NB: after a successful run, V will delete the generated executable.
2021-03-04 22:34:16 +03:00
If you want to keep it, use `v -keepc run .` instead, or just compile
manually with `v .` .
2021-03-04 22:52:55 +03:00
NB: any V compiler flags should be passed *before* the `run` command.
2021-03-09 19:24:21 +03:00
Everything after the source file/folder, will be passed to the program
2021-03-04 22:52:55 +03:00
as is - it will not be processed by V.
2021-03-04 22:34:16 +03:00
2019-11-21 04:36:55 +03:00
## Comments
```v
// This is a single line comment.
2020-12-06 00:54:41 +03:00
/*
This is a multiline comment.
2019-11-21 04:36:55 +03:00
/* It can be nested. */
*/
```
## Functions
```v
fn main() {
2020-12-06 00:54:41 +03:00
println(add(77, 33))
println(sub(100, 50))
2019-11-21 04:36:55 +03:00
}
fn add(x int, y int) int {
2020-12-06 00:54:41 +03:00
return x + y
2019-11-21 04:36:55 +03:00
}
2020-10-15 13:32:28 +03:00
fn sub(x int, y int) int {
2020-12-06 00:54:41 +03:00
return x - y
2019-11-21 04:36:55 +03:00
}
```
Again, the type comes after the argument's name.
Just like in Go and C, functions cannot be overloaded.
This simplifies the code and improves maintainability and readability.
2021-09-30 12:43:26 +03:00
### Hoistings
2019-11-21 04:36:55 +03:00
Functions can be used before their declaration:
`add` and `sub` are declared after `main` , but can still be called from `main` .
2020-06-30 15:29:55 +03:00
This is true for all declarations in V and eliminates the need for header files
2019-11-21 04:36:55 +03:00
or thinking about the order of files and declarations.
2020-07-23 19:55:54 +03:00
### Returning multiple values
2020-04-16 03:01:55 +03:00
2021-01-10 22:21:37 +03:00
```v
2019-11-21 04:36:55 +03:00
fn foo() (int, int) {
2021-01-10 22:21:37 +03:00
return 2, 3
2019-11-21 04:36:55 +03:00
}
a, b := foo()
println(a) // 2
println(b) // 3
2020-07-14 20:24:59 +03:00
c, _ := foo() // ignore values using `_ `
2019-11-21 04:36:55 +03:00
```
2020-07-23 19:55:54 +03:00
## Symbol visibility
2019-11-21 04:36:55 +03:00
```v
pub fn public_function() {
}
fn private_function() {
}
```
2020-07-23 19:55:54 +03:00
Functions are private (not exported) by default.
2020-05-11 21:21:41 +03:00
To allow other modules to use them, prepend `pub` . The same applies
to constants and types.
2020-10-11 21:36:41 +03:00
Note: `pub` can only be used from a named module.
For information about creating a module, see [Modules ](#modules ).
2020-05-11 21:25:48 +03:00
## Variables
2019-11-21 04:36:55 +03:00
```v
name := 'Bob'
age := 20
large_number := i64(9999999999)
println(name)
println(age)
println(large_number)
```
Variables are declared and initialized with `:=` . This is the only
way to declare variables in V. This means that variables always have an initial
value.
The variable's type is inferred from the value on the right hand side.
2020-06-30 15:29:55 +03:00
To choose a different type, use type conversion:
2019-11-21 04:36:55 +03:00
the expression `T(v)` converts the value `v` to the
type `T` .
Unlike most other languages, V only allows defining variables in functions.
2020-07-23 19:55:54 +03:00
Global (module level) variables are not allowed. There's no global state in V
(see [Pure functions by default ](#pure-functions-by-default ) for details).
2019-11-21 04:36:55 +03:00
2020-09-14 17:06:32 +03:00
For consistency across different code bases, all variable and function names
must use the `snake_case` style, as opposed to type names, which must use `PascalCase` .
2020-07-23 19:55:54 +03:00
### Mutable variables
2020-05-11 21:25:48 +03:00
2019-11-21 04:36:55 +03:00
```v
2020-04-21 06:11:50 +03:00
mut age := 20
2019-11-21 04:36:55 +03:00
println(age)
age = 21
println(age)
```
To change the value of the variable use `=` . In V, variables are
2020-10-20 21:14:56 +03:00
immutable by default.
To be able to change the value of the variable, you have to declare it with `mut` .
2019-11-21 04:36:55 +03:00
2020-04-21 06:11:50 +03:00
Try compiling the program above after removing `mut` from the first line.
2019-11-21 04:36:55 +03:00
2020-07-23 19:55:54 +03:00
### Initialization vs assignment
2020-09-07 14:50:12 +03:00
Note the (important) difference between `:=` and `=` .
2019-11-21 04:36:55 +03:00
`:=` is used for declaring and initializing, `=` is used for assigning.
2020-11-03 03:04:14 +03:00
```v failcompile
2019-11-21 04:36:55 +03:00
fn main() {
2021-04-12 15:13:00 +03:00
age = 21
2019-11-21 04:36:55 +03:00
}
```
2020-05-07 13:30:41 +03:00
This code will not compile, because the variable `age` is not declared.
2019-11-21 04:36:55 +03:00
All variables need to be declared in V.
```v
fn main() {
2020-12-06 00:54:41 +03:00
age := 21
2019-11-21 04:36:55 +03:00
}
```
2021-01-24 01:41:47 +03:00
The values of multiple variables can be changed in one line.
In this way, their values can be swapped without an intermediary variable.
```v
mut a := 0
mut b := 1
println('$a, $b') // 0, 1
a, b = b, a
println('$a, $b') // 1, 0
```
2020-06-30 15:29:55 +03:00
### Declaration errors
2020-10-20 21:14:56 +03:00
In development mode the compiler will warn you that you haven't used the variable
(you'll get an "unused variable" warning).
In production mode (enabled by passing the `-prod` flag to v – `v -prod foo.v` )
it will not compile at all (like in Go).
2019-11-21 04:36:55 +03:00
2021-04-12 15:13:00 +03:00
```v failcompile nofmt
2019-11-21 04:36:55 +03:00
fn main() {
2021-04-12 15:13:00 +03:00
a := 10
if true {
a := 20 // error: redefinition of `a`
}
// warning: unused variable `a`
2019-11-21 04:36:55 +03:00
}
```
2020-10-20 21:14:56 +03:00
Unlike most languages, variable shadowing is not allowed. Declaring a variable with a name
that is already used in a parent scope will cause a compilation error.
2019-11-21 04:36:55 +03:00
2020-12-01 18:09:33 +03:00
You can shadow imported modules though, as it is very useful in some situations:
```v ignore
import ui
import gg
fn draw(ctx & gg.Context) {
2021-04-12 15:13:00 +03:00
gg := ctx.parent.get_ui().gg
gg.draw_rect(10, 10, 100, 50)
2020-12-01 18:09:33 +03:00
}
```
2021-06-22 12:39:57 +03:00
## V Types
2020-06-26 20:36:40 +03:00
### Primitive types
2019-11-21 04:36:55 +03:00
2020-11-03 03:04:14 +03:00
```v ignore
2019-11-21 04:36:55 +03:00
bool
string
i8 i16 int i64 i128 (soon)
byte u16 u32 u64 u128 (soon)
rune // represents a Unicode code point
f32 f64
2021-09-08 05:53:39 +03:00
isize, usize // platform-dependent, the size is how many bytes it takes to reference any location in memory
2021-09-08 13:09:32 +03:00
voidptr // this one is mostly used for C interoperability
2020-05-17 17:11:48 +03:00
any // similar to C's void* and Go's interface{}
2019-11-21 04:36:55 +03:00
```
Please note that unlike C and Go, `int` is always a 32 bit integer.
2020-09-15 16:50:35 +03:00
There is an exception to the rule that all operators
2020-05-27 06:42:48 +03:00
in V must have values of the same type on both sides. A small primitive type
on one side can be automatically promoted if it fits
2020-06-02 18:00:14 +03:00
completely into the data range of the type on the other side.
2020-05-27 06:42:48 +03:00
These are the allowed possibilities:
2020-11-03 03:04:14 +03:00
```v ignore
2020-05-27 06:42:48 +03:00
i8 → i16 → int → i64
2020-06-02 18:00:14 +03:00
↘ ↘
f32 → f64
↗ ↗
2020-05-27 06:42:48 +03:00
byte → u16 → u32 → u64 ⬎
↘ ↘ ↘ ptr
i8 → i16 → int → i64 ⬏
```
An `int` value for example can be automatically promoted to `f64`
2021-03-23 12:01:24 +03:00
or `i64` but not to `u32` . (`u32` would mean loss of the sign for
2020-05-27 06:42:48 +03:00
negative values).
2021-03-23 12:01:24 +03:00
Promotion from `int` to `f32` , however, is currently done automatically
(but can lead to precision loss for large values).
2020-05-27 06:42:48 +03:00
2021-01-12 00:58:15 +03:00
Literals like `123` or `4.56` are treated in a special way. They do
not lead to type promotions, however they default to `int` and `f64`
respectively, when their type has to be decided:
2021-02-05 20:50:28 +03:00
```v nofmt
2021-01-12 00:58:15 +03:00
u := u16(12)
v := 13 + u // v is of type `u16` - no promotion
x := f32(45.6)
y := x + 3.14 // x is of type `f32` - no promotion
a := 75 // a is of type `int` - default for int literal
b := 14.7 // b is of type `f64` - default for float literal
c := u + a // c is of type `int` - automatic promotion of `u` 's value
2021-01-13 13:50:35 +03:00
d := b + x // d is of type `f64` - automatic promotion of `x` 's value
2021-01-12 00:58:15 +03:00
```
2020-06-26 20:36:40 +03:00
### Strings
2019-11-21 04:36:55 +03:00
2021-01-10 22:21:37 +03:00
```v
2019-11-21 04:36:55 +03:00
name := 'Bob'
println(name.len)
2020-10-11 21:36:01 +03:00
println(name[0]) // indexing gives a byte B
println(name[1..3]) // slicing gives a string 'ob'
windows_newline := '\r\n' // escape special characters like in C
assert windows_newline.len == 2
2019-11-21 04:36:55 +03:00
```
2021-10-31 12:32:31 +03:00
In V, a string is a read-only array of bytes. String data is encoded using UTF-8:
```v
s := 'hello 🌎' // emoji takes 4 bytes
assert s.len == 10
arr := s.bytes() // convert `string` to `[]byte`
assert arr.len == 10
s2 := arr.bytestr() // convert `[]byte` to `string`
assert s2 == s
```
2020-10-11 21:36:01 +03:00
String values are immutable. You cannot mutate elements:
2019-11-21 04:36:55 +03:00
2020-11-03 03:04:14 +03:00
```v failcompile
2020-10-11 21:36:01 +03:00
mut s := 'hello 🌎'
s[0] = `H` // not allowed
```
> error: cannot assign to `s[i]` since V strings are immutable
2021-10-31 15:12:04 +03:00
Note that indexing a string will produce a `byte` , not a `rune` nor another `string` .
Indexes correspond to bytes in the string, not Unicode code points. If you want to
2021-08-08 04:52:05 +03:00
convert the `byte` to a `string` , use the `ascii_str()` method:
```v
country := 'Netherlands'
println(country[0]) // Output: 78
println(country[0].ascii_str()) // Output: N
```
2020-10-11 21:36:01 +03:00
Character literals have type `rune` . To denote them, use `
2020-09-14 17:18:10 +03:00
```v
2020-10-11 21:36:01 +03:00
rocket := `🚀`
assert 'aloha!'[0] == `a`
2020-09-14 17:18:10 +03:00
```
2019-11-21 04:36:55 +03:00
Both single and double quotes can be used to denote strings. For consistency,
`vfmt` converts double quotes to single quotes unless the string contains a single quote character.
2020-10-11 21:36:01 +03:00
For raw strings, prepend `r` . Raw strings are not escaped:
2020-05-02 01:43:59 +03:00
2021-01-10 22:21:37 +03:00
```v
2020-10-11 21:36:01 +03:00
s := r'hello\nworld'
println(s) // "hello\nworld"
2020-05-02 01:43:59 +03:00
```
2021-01-20 11:30:26 +03:00
Strings can be easily converted to integers:
2021-01-20 08:16:39 +03:00
```v
s := '42'
n := s.int() // 42
```
2021-06-30 22:30:28 +03:00
### Runes
A `rune` represents a unicode character and is an alias for `u32` . Runes can be created like this:
```v
x := `🚀`
```
A string can be converted to runes by the `.runes()` method.
```v
hello := 'Hello World 👋'
hello_runes := hello.runes() // [`H`, `e` , `l` , `l` , `o` , ` ` , `W` , `o` , `r` , `l` , `d` , ` ` , `👋` ]
```
2020-10-11 21:36:01 +03:00
### String interpolation
2019-11-21 04:36:55 +03:00
2020-10-11 21:36:01 +03:00
Basic interpolation syntax is pretty simple - use `$` before a variable name.
The variable will be converted to a string and embedded into the literal:
2021-01-10 22:21:37 +03:00
```v
2020-10-11 21:36:01 +03:00
name := 'Bob'
println('Hello, $name!') // Hello, Bob!
2019-11-21 04:36:55 +03:00
```
2020-10-11 21:36:01 +03:00
It also works with fields: `'age = $user.age'` .
If you need more complex expressions, use `${}` : `'can register = ${user.age > 13}'` .
2019-11-21 04:36:55 +03:00
2020-10-20 21:14:56 +03:00
Format specifiers similar to those in C's `printf()` are also supported.
`f` , `g` , `x` , etc. are optional and specify the output format.
The compiler takes care of the storage size, so there is no `hd` or `llu` .
2019-11-21 04:36:55 +03:00
2021-01-10 22:21:37 +03:00
```v
2020-10-11 21:36:01 +03:00
x := 123.4567
println('x = ${x:4.2f}')
2021-01-23 10:02:28 +03:00
println('[${x:10}]') // pad with spaces on the left => [ 123.457]
println('[${int(x):-10}]') // pad with spaces on the right => [123 ]
println('[${int(x):010}]') // pad with zeros on the left => [0000000123]
2019-11-21 04:36:55 +03:00
```
2020-10-11 21:36:01 +03:00
### String operators
2019-11-21 04:36:55 +03:00
2021-01-10 22:21:37 +03:00
```v
2020-11-03 03:04:14 +03:00
name := 'Bob'
2020-10-11 21:36:01 +03:00
bobby := name + 'by' // + is used to concatenate strings
println(bobby) // "Bobby"
mut s := 'hello '
s += 'world' // `+=` is used to append to a string
println(s) // "hello world"
```
2021-01-10 22:21:37 +03:00
2020-10-11 21:36:01 +03:00
All operators in V must have values of the same type on both sides.
You cannot concatenate an integer to a string:
2020-11-03 03:04:14 +03:00
```v failcompile
2020-10-11 21:36:01 +03:00
age := 10
println('age = ' + age) // not allowed
2019-11-21 04:36:55 +03:00
```
2020-10-11 21:36:01 +03:00
> error: infix expr: cannot use `int` (right expression) as `string`
2019-11-21 04:36:55 +03:00
2020-10-11 21:36:01 +03:00
We have to either convert `age` to a `string` :
2019-11-21 04:36:55 +03:00
```v
2020-11-03 03:04:14 +03:00
age := 11
2020-10-11 21:36:01 +03:00
println('age = ' + age.str())
2019-11-21 04:36:55 +03:00
```
2020-10-11 21:36:01 +03:00
or use string interpolation (preferred):
2019-11-21 04:36:55 +03:00
```v
2020-11-03 03:04:14 +03:00
age := 12
2020-10-11 21:36:01 +03:00
println('age = $age')
2019-11-21 04:36:55 +03:00
```
2020-07-14 11:10:36 +03:00
### Numbers
```v
a := 123
```
2020-07-14 16:46:13 +03:00
2020-07-14 11:10:36 +03:00
This will assign the value of 123 to `a` . By default `a` will have the
2020-07-14 11:38:40 +03:00
type `int` .
2020-07-14 11:10:36 +03:00
2020-07-14 20:26:14 +03:00
You can also use hexadecimal, binary or octal notation for integer literals:
2020-07-14 16:46:13 +03:00
2020-07-14 11:10:36 +03:00
```v
a := 0x7B
2020-07-14 20:26:14 +03:00
b := 0b01111011
c := 0o173
2020-07-14 11:10:36 +03:00
```
2020-07-14 20:26:14 +03:00
All of these will be assigned the same value, 123. They will all have type
`int` , no matter what notation you used.
2020-07-14 11:10:36 +03:00
2020-07-14 16:46:13 +03:00
V also supports writing numbers with `_` as separator:
2021-01-10 22:21:37 +03:00
```v
2020-07-14 16:46:13 +03:00
num := 1_000_000 // same as 1000000
three := 0b0_11 // same as 0b11
2021-01-10 22:21:37 +03:00
float_num := 3_122.55 // same as 3122.55
2020-07-14 16:46:13 +03:00
hexa := 0xF_F // same as 255
oct := 0o17_3 // same as 0o173
```
2020-07-14 11:10:36 +03:00
If you want a different type of integer, you can use casting:
2020-07-14 16:46:13 +03:00
2020-07-14 11:10:36 +03:00
```v
a := i64(123)
b := byte(42)
c := i16(12345)
```
Assigning floating point numbers works the same way:
2020-07-14 16:46:13 +03:00
2020-07-14 11:10:36 +03:00
```v
f := 1.0
f1 := f64(3.14)
f2 := f32(3.14)
```
2020-08-12 20:14:13 +03:00
If you do not specify the type explicitly, by default float literals
2020-07-14 11:10:36 +03:00
will have the type of `f64` .
2021-07-28 08:21:42 +03:00
Float literals can also be declared as a power of ten:
```v
f0 := 42e1 // 420
f1 := 123e-2 // 1.23
f2 := 456e+2 // 45600
```
2020-06-26 20:36:40 +03:00
### Arrays
2021-05-30 14:48:33 +03:00
#### Basic Array Concepts
Arrays are collections of data elements of the same type. They can be represented by
a list of elements surrounded by brackets. The elements can be accessed by appending
an *index* (starting with `0` ) in brackets to the array variable:
2021-01-10 22:21:37 +03:00
```v
2020-04-21 06:11:50 +03:00
mut nums := [1, 2, 3]
2021-05-30 14:48:33 +03:00
println(nums) // `[1, 2, 3]`
println(nums[0]) // `1`
println(nums[1]) // `2`
2020-06-30 15:37:02 +03:00
nums[1] = 5
2021-05-30 14:48:33 +03:00
println(nums) // `[1, 5, 3]`
```
#### Array Properties
There are two properties that control the "size" of an array:
2021-07-09 23:53:25 +03:00
* `len` : *length* - the number of pre-allocated and initialized elements in the array
* `cap` : *capacity* - the amount of memory space which has been reserved for elements,
but not initialized or counted as elements. The array can grow up to this size without
being reallocated. Usually, V takes care of this property automatically but there are
cases where the user may want to do manual optimizations (see [below ](#array-initialization )).
2021-05-30 14:48:33 +03:00
```v
mut nums := [1, 2, 3]
2020-06-30 15:37:02 +03:00
println(nums.len) // "3"
2021-05-30 14:48:33 +03:00
println(nums.cap) // "3" or greater
2020-06-30 15:37:02 +03:00
nums = [] // The array is now empty
println(nums.len) // "0"
```
2021-05-30 14:48:33 +03:00
Note that the properties are read-only fields and can't be modified by the user.
#### Array Initialization
The basic initialization syntax is as described [above ](#basic-array-concepts ).
2020-06-30 15:37:02 +03:00
The type of an array is determined by the first element:
* `[1, 2, 3]` is an array of ints (`[]int`).
* `['a', 'b']` is an array of strings (`[]string`).
2020-12-16 20:23:00 +03:00
The user can explicitly specify the type for the first element: `[byte(16), 32, 64, 128]` .
2020-10-20 21:14:56 +03:00
V arrays are homogeneous (all elements must have the same type).
This means that code like `[1, 'a']` will not compile.
2020-06-30 15:37:02 +03:00
2021-05-30 14:48:33 +03:00
The above syntax is fine for a small number of known elements but for very large or empty
arrays there is a second initialization syntax:
```v
mut a := []int{len: 10000, cap: 30000, init: 3}
```
2021-10-15 03:57:49 +03:00
2021-05-30 14:48:33 +03:00
This creates an array of 10000 `int` elements that are all initialized with `3` . Memory
space is reserved for 30000 elements. The parameters `len` , `cap` and `init` are optional;
`len` defaults to `0` and `init` to the default initialization of the element type (`0`
for numerical type, `''` for `string` , etc). The run time system makes sure that the
capacity is not smaller than `len` (even if a smaller value is specified explicitly):
```v
arr := []int{len: 5, init: -1}
// `arr == [-1, -1, -1, -1, -1]` , arr.cap == 5
2020-06-30 15:37:02 +03:00
2021-05-30 14:48:33 +03:00
// Declare an empty array:
users := []int{}
```
Setting the capacity improves performance of pushing elements to the array
as reallocations can be avoided:
```v
mut numbers := []int{cap: 1000}
println(numbers.len) // 0
// Now appending elements won't reallocate
for i in 0 .. 1000 {
numbers < < i
}
```
Note: The above code uses a [range `for` ](#range-for ) statement and a
[push operator (`<<`) ](#array-operations ).
2021-10-22 07:23:45 +03:00
You can initialize the array by accessing the `it` variable as shown here:
```v
mut square := []int{len: 6, init: it * it}
// square == [0, 1, 4, 9, 16, 25]
```
2021-06-08 20:35:22 +03:00
#### Array Types
An array can be of these types:
| Types | Example Definition |
| ------------ | ------------------------------------ |
| Number | `[]int,[]i64` |
| String | `[]string` |
| Rune | `[]rune` |
| Boolean | `[]bool` |
| Array | `[][]int` |
| Struct | `[]MyStructName` |
| Channel | `[]chan f64` |
| Function | `[]MyFunctionType` `[]fn (int) bool` |
| Interface | `[]MyInterfaceName` |
| Sum Type | `[]MySumTypeName` |
| Generic Type | `[]T` |
| Map | `[]map[string]f64` |
| Enum | `[]MyEnumType` |
| Alias | `[]MyAliasTypeName` |
| Thread | `[]thread int` |
| Reference | `[]&f64` |
| Shared | `[]shared MyStructType` |
**Example Code:**
2021-06-25 12:11:29 +03:00
This example uses [Structs ](#structs ) and [Sum Types ](#sum-types ) to create an array
2021-06-08 20:35:22 +03:00
which can handle different types (e.g. Points, Lines) of data elements.
```v
struct Point {
x int
y int
}
struct Line {
p1 Point
p2 Point
}
type ObjectSumType = Line | Point
mut object_list := []ObjectSumType{}
object_list < < Point { 1 , 1 }
object_list < < Line {
p1: Point{3, 3}
p2: Point{4, 4}
}
dump(object_list)
/*
object_list: [ObjectSumType(Point{
x: 1
y: 1
}), ObjectSumType(Line{
p1: Point{
x: 3
y: 3
}
p2: Point{
x: 4
y: 4
}
})]
*/
```
2021-05-30 14:48:33 +03:00
#### Multidimensional Arrays
Arrays can have more than one dimension.
2d array example:
```v
mut a := [][]int{len: 2, init: []int{len: 3}}
a[0][1] = 2
println(a) // [[0, 2, 0], [0, 0, 0]]
```
3d array example:
```v
mut a := [][][]int{len: 2, init: [][]int{len: 3, init: []int{len: 2}}}
a[0][1][1] = 2
println(a) // [[[0, 0], [0, 2], [0, 0]], [[0, 0], [0, 0], [0, 0]]]
```
#### Array Operations
Elements can be appended to the end of an array using the push operator `<<` .
It can also append an entire array.
2020-07-26 21:29:51 +03:00
2021-01-10 22:21:37 +03:00
```v
2020-06-30 15:37:02 +03:00
mut nums := [1, 2, 3]
2019-11-21 04:36:55 +03:00
nums < < 4
println(nums) // "[1, 2, 3, 4]"
2020-06-30 15:37:02 +03:00
// append array
2019-11-21 04:36:55 +03:00
nums < < [5, 6, 7]
println(nums) // "[1, 2, 3, 4, 5, 6, 7]"
2020-04-21 06:11:50 +03:00
mut names := ['John']
2019-11-21 04:36:55 +03:00
names < < 'Peter'
names < < 'Sam'
// names < < 10 < -- This will not compile . `names` is an array of strings .
```
2020-06-30 15:37:02 +03:00
`val in array` returns true if the array contains `val` . See [`in` operator ](#in-operator ).
2019-11-21 04:36:55 +03:00
2020-07-26 21:29:51 +03:00
```v
2021-05-30 14:48:33 +03:00
names := ['John', 'Peter', 'Sam']
println(names.len) // "3"
println('Alex' in names) // "false"
2020-07-26 21:29:51 +03:00
```
2020-07-07 12:55:22 +03:00
2020-06-30 15:37:02 +03:00
#### Array methods
2019-11-21 04:36:55 +03:00
All arrays can be easily printed with `println(arr)` and converted to a string
with `s := arr.str()` .
2020-12-14 02:37:09 +03:00
Copying the data from the array is done with `.clone()` :
2021-01-10 22:21:37 +03:00
```v
2020-12-14 02:37:09 +03:00
nums := [1, 2, 3]
nums_copy := nums.clone()
```
2020-05-07 13:30:41 +03:00
Arrays can be efficiently filtered and mapped with the `.filter()` and
2019-11-21 04:36:55 +03:00
`.map()` methods:
2021-01-10 22:21:37 +03:00
```v
2019-11-21 04:36:55 +03:00
nums := [1, 2, 3, 4, 5, 6]
even := nums.filter(it % 2 == 0)
println(even) // [2, 4, 6]
2020-12-02 16:41:31 +03:00
// filter can accept anonymous functions
even_fn := nums.filter(fn (x int) bool {
return x % 2 == 0
})
println(even_fn)
2019-11-21 04:36:55 +03:00
words := ['hello', 'world']
upper := words.map(it.to_upper())
println(upper) // ['HELLO', 'WORLD']
2020-12-02 16:41:31 +03:00
// map can also accept anonymous functions
upper_fn := words.map(fn (w string) string {
return w.to_upper()
})
println(upper_fn) // ['HELLO', 'WORLD']
2019-11-21 04:36:55 +03:00
```
2020-05-07 19:01:09 +03:00
`it` is a builtin variable which refers to element currently being processed in filter/map methods.
2019-11-21 04:36:55 +03:00
2021-03-20 19:27:27 +03:00
Additionally, `.any()` and `.all()` can be used to conveniently test
for elements that satisfy a condition.
```v
nums := [1, 2, 3]
println(nums.any(it == 2)) // true
println(nums.all(it >= 2)) // false
```
2021-05-30 14:48:33 +03:00
There are further built in methods for arrays:
* `b := a.repeat(n)` concatenate `n` times the elements of `a`
* `a.insert(i, val)` insert new element `val` at index `i` and move all following elements upwards
* `a.insert(i, [3, 4, 5])` insert several elements
* `a.prepend(val)` insert value at beginning, equivalent to `a.insert(0, val)`
* `a.prepend(arr)` insert elements of array `arr` at beginning
* `a.trim(new_len)` truncate the length (if `new_length < a.len` , otherwise do nothing)
* `a.clear()` empty the array (without changing `cap` , equivalent to `a.trim(0)` )
2021-07-21 20:55:32 +03:00
* `a.delete_many(start, size)` removes `size` consecutive elements beginning with index `start`
– triggers reallocation
* `a.delete(index)` equivalent to `a.delete_many(index, 1)`
2021-05-30 14:48:33 +03:00
* `v := a.first()` equivalent to `v := a[0]`
* `v := a.last()` equivalent to `v := a[a.len - 1]`
* `v := a.pop()` get last element and remove it from array
* `a.delete_last()` remove last element from array
* `b := a.reverse()` make `b` contain the elements of `a` in reversed order
* `a.reverse_in_place()` reverse the order of elements in `a`
2021-06-06 12:32:49 +03:00
* `a.join(joiner)` concatenate array of strings into a string using `joiner` string as a separator
2020-08-05 02:46:04 +03:00
2021-05-30 14:48:33 +03:00
#### Sorting Arrays
2020-08-12 20:14:13 +03:00
Sorting arrays of all kinds is very simple and intuitive. Special variables `a` and `b`
are used when providing a custom sorting condition.
2021-01-13 21:10:17 +03:00
```v
2020-08-12 20:14:13 +03:00
mut numbers := [1, 3, 2]
2021-01-10 22:21:37 +03:00
numbers.sort() // 1, 2, 3
2020-08-12 20:14:13 +03:00
numbers.sort(a > b) // 3, 2, 1
```
2021-02-05 20:50:28 +03:00
```v
2021-01-10 22:21:37 +03:00
struct User {
age int
name string
}
2020-10-21 12:57:58 +03:00
mut users := [User{21, 'Bob'}, User{20, 'Zarkon'}, User{25, 'Alice'}]
2021-01-10 22:21:37 +03:00
users.sort(a.age < b.age ) / / sort by User . age int field
2020-08-12 20:14:13 +03:00
users.sort(a.name > b.name) // reverse sort by User.name string field
```
2021-07-16 11:57:37 +03:00
V also supports custom sorting, through the `sort_with_compare` array method.
Which expects a comparing function which will define the sort order.
Useful for sorting on multiple fields at the same time by custom sorting rules.
The code below sorts the array ascending on `name` and descending `age` .
```v
struct User {
age int
name string
}
mut users := [User{21, 'Bob'}, User{65, 'Bob'}, User{25, 'Alice'}]
custom_sort_fn := fn (a & User, b & User) int {
// return -1 when a comes before b
// return 0, when both are in same order
// return 1 when b comes before a
if a.name == b.name {
if a.age < b.age {
return 1
}
if a.age > b.age {
return -1
}
return 0
}
if a.name < b.name {
return -1
} else if a.name > b.name {
return 1
}
return 0
}
users.sort_with_compare(custom_sort_fn)
```
2020-08-12 20:14:13 +03:00
2021-01-30 13:49:06 +03:00
#### Array Slices
2021-05-30 14:48:33 +03:00
A slice is a part of a parent array. Initially it refers to the elements
between two indices separated by a `..` operator. The right-side index must
be greater than or equal to the left side index.
2021-01-30 13:49:06 +03:00
If a right-side index is absent, it is assumed to be the array length. If a
left-side index is absent, it is assumed to be 0.
```v
2021-03-14 19:43:08 +03:00
nums := [0, 10, 20, 30, 40]
println(nums[1..4]) // [10, 20, 30]
println(nums[..4]) // [0, 10, 20, 30]
println(nums[1..]) // [10, 20, 30, 40]
2021-01-30 13:49:06 +03:00
```
2021-09-11 04:46:51 +03:00
In V slices are arrays themselves (they are not distinct types). As a result
2021-05-30 14:48:33 +03:00
all array operations may be performed on them. E.g. they can be pushed onto an
array of the same type:
2021-01-30 13:49:06 +03:00
```v
array_1 := [3, 5, 4, 7, 6]
mut array_2 := [0, 1]
array_2 < < array_1 [ . . 3 ]
2021-05-30 14:48:33 +03:00
println(array_2) // `[0, 1, 3, 5, 4]`
```
A slice is always created with the smallest possible capacity `cap == len` (see
[`cap` above ](#array-initialization )) no matter what the capacity or length
of the parent array is. As a result it is immediately reallocated and copied to another
memory location when the size increases thus becoming independent from the
parent array (*copy on grow*). In particular pushing elements to a slice
does not alter the parent:
```v
mut a := [0, 1, 2, 3, 4, 5]
mut b := a[2..4]
b[0] = 7 // `b[0]` is referring to `a[2]`
println(a) // `[0, 1, 7, 3, 4, 5]`
b < < 9
// `b` has been reallocated and is now independent from `a`
println(a) // `[0, 1, 7, 3, 4, 5]` - no change
println(b) // `[7, 3, 9]`
```
Appending to the parent array may or may not make it independent from its child slices.
The behaviour depends on the parent's capacity and is predictable:
```v
mut a := []int{len: 5, cap: 6, init: 2}
mut b := a[1..4]
a < < 3
// no reallocation - fits in `cap`
b[2] = 13 // `a[3]` is modified
a < < 4
// a has been reallocated and is now independent from `b` (`cap` was exceeded)
b[1] = 3 // no change in `a`
println(a) // `[2, 2, 2, 13, 2, 3, 4]`
println(b) // `[2, 3, 13]`
2021-01-30 13:49:06 +03:00
```
2021-02-02 20:51:40 +03:00
### Fixed size arrays
2021-02-05 10:05:13 +03:00
V also supports arrays with fixed size. Unlike ordinary arrays, their
2021-02-02 20:51:40 +03:00
length is constant. You cannot append elements to them, nor shrink them.
You can only modify their elements in place.
2021-02-01 20:11:17 +03:00
2021-02-02 20:51:40 +03:00
However, access to the elements of fixed size arrays is more efficient,
2021-02-01 20:11:17 +03:00
they need less memory than ordinary arrays, and unlike ordinary arrays,
their data is on the stack, so you may want to use them as buffers if you
do not want additional heap allocations.
2021-02-02 20:51:40 +03:00
Most methods are defined to work on ordinary arrays, not on fixed size arrays.
You can convert a fixed size array to an ordinary array with slicing:
2021-02-01 20:11:17 +03:00
```v
2021-02-02 20:51:40 +03:00
mut fnums := [3]int{} // fnums is a fixed size array with 3 elements.
2021-02-01 20:11:17 +03:00
fnums[0] = 1
fnums[1] = 10
fnums[2] = 100
println(fnums) // => [1, 10, 100]
println(typeof(fnums).name) // => [3]int
2021-02-02 20:51:40 +03:00
2021-04-30 15:59:38 +03:00
fnums2 := [1, 10, 100]! // short init syntax that does the same (the syntax will probably change)
2021-10-31 12:32:31 +03:00
anums := fnums[..] // same as `anums := fnums[0..fnums.len]`
2021-02-01 20:11:17 +03:00
println(anums) // => [1, 10, 100]
println(typeof(anums).name) // => []int
```
2021-02-02 20:51:40 +03:00
Note that slicing will cause the data of the fixed size array to be copied to
2021-02-01 20:11:17 +03:00
the newly created ordinary array.
2020-06-26 20:36:40 +03:00
### Maps
2019-11-21 04:36:55 +03:00
2021-01-10 22:21:37 +03:00
```v
2021-01-19 08:14:19 +03:00
mut m := map[string]int{} // a map with `string` keys and `int` values
2019-11-21 04:36:55 +03:00
m['one'] = 1
m['two'] = 2
println(m['one']) // "1"
println(m['bad_key']) // "0"
println('bad_key' in m) // Use `in` to detect whether such key exists
2021-09-05 18:23:04 +03:00
println(m.keys()) // ['one', 'two']
2019-11-21 04:36:55 +03:00
m.delete('two')
2021-02-08 17:57:42 +03:00
```
Maps can have keys of type string, rune, integer, float or voidptr.
The whole map can be initialized using this short syntax:
```v
2021-08-04 12:44:41 +03:00
numbers := {
2021-05-01 12:51:50 +03:00
'one': 1
'two': 2
2019-11-21 04:36:55 +03:00
}
2021-01-19 09:42:56 +03:00
println(numbers)
2019-11-21 04:36:55 +03:00
```
2021-01-19 08:14:19 +03:00
If a key is not found, a zero value is returned by default:
2021-01-19 08:11:18 +03:00
2021-01-19 08:14:19 +03:00
```v
2021-08-04 12:44:41 +03:00
sm := {
2021-01-19 09:42:56 +03:00
'abc': 'xyz'
}
val := sm['bad_key']
println(val) // ''
2021-02-08 17:57:42 +03:00
```
```v
2021-08-04 12:44:41 +03:00
intm := {
2021-01-19 09:42:56 +03:00
1: 1234
2: 5678
}
s := intm[3]
println(s) // 0
2021-01-19 08:11:18 +03:00
```
2021-01-19 08:14:19 +03:00
It's also possible to use an `or {}` block to handle missing keys:
2021-01-19 08:11:18 +03:00
2021-01-19 08:14:19 +03:00
```v
2021-01-19 09:42:56 +03:00
mm := map[string]int{}
val := mm['bad_key'] or { panic('key not found') }
2021-01-19 08:11:18 +03:00
```
The same optional check applies to arrays:
2021-01-19 08:14:19 +03:00
```v
2021-01-19 09:42:56 +03:00
arr := [1, 2, 3]
large_index := 999
val := arr[large_index] or { panic('out of bounds') }
2021-10-25 17:19:24 +03:00
println(val)
// you can also do this, if you want to *propagate* the access error:
val2 := arr[333] ?
println(val2)
2021-01-19 08:11:18 +03:00
```
2020-07-23 19:55:54 +03:00
## Module imports
2020-07-18 22:34:38 +03:00
2020-10-11 21:36:41 +03:00
For information about creating a module, see [Modules ](#modules ).
2020-07-18 22:34:38 +03:00
2020-10-11 21:36:41 +03:00
Modules can be imported using the `import` keyword:
2020-06-26 20:36:40 +03:00
```v
import os
fn main() {
2020-12-06 00:54:41 +03:00
// read text from stdin
name := os.input('Enter your name: ')
println('Hello, $name!')
2020-06-26 20:36:40 +03:00
}
```
2020-10-15 13:32:28 +03:00
This program can use any public definitions from the `os` module, such
2020-10-11 21:36:41 +03:00
as the `input` function. See the [standard library ](https://modules.vlang.io/ )
documentation for a list of common modules and their public symbols.
2020-06-26 20:36:40 +03:00
2020-10-11 21:36:41 +03:00
By default, you have to specify the module prefix every time you call an external function.
This may seem verbose at first, but it makes code much more readable
and easier to understand - it's always clear which function from
which module is being called. This is especially useful in large code bases.
2021-01-05 20:33:59 +03:00
Cyclic module imports are not allowed, like in Go.
2020-10-11 21:36:41 +03:00
### Selective imports
You can also import specific functions and types from modules directly:
2020-07-18 22:34:38 +03:00
2021-01-10 22:19:31 +03:00
```v
2020-07-18 22:34:38 +03:00
import os { input }
2021-02-07 05:58:43 +03:00
fn main() {
// read text from stdin
name := input('Enter your name: ')
println('Hello, $name!')
}
2020-07-18 22:34:38 +03:00
```
2021-06-26 18:10:30 +03:00
Note: This will import the module as well. Also, this is not allowed for
constants - they must always be prefixed.
2020-10-11 21:36:41 +03:00
You can import several specific symbols at once:
```v
import os { input, user_os }
name := input('Enter your name: ')
println('Name: $name')
os := user_os()
println('Your OS is ${os}.')
```
2020-07-18 22:34:38 +03:00
2020-07-23 19:55:54 +03:00
### Module import aliasing
2020-07-18 22:34:38 +03:00
Any imported module name can be aliased using the `as` keyword:
NOTE: this example will not compile unless you have created `mymod/sha256.v`
2020-11-03 03:04:14 +03:00
```v failcompile
2020-07-18 22:34:38 +03:00
import crypto.sha256
import mymod.sha256 as mysha256
fn main() {
2021-08-02 07:11:03 +03:00
v_hash := sha256.sum('hi'.bytes()).hex()
2021-04-12 15:13:00 +03:00
my_hash := mysha256.sum('hi'.bytes()).hex()
assert my_hash == v_hash
2020-07-18 22:34:38 +03:00
}
```
You cannot alias an imported function or type.
However, you _can_ redeclare a type.
```v
import time
2020-12-25 18:19:00 +03:00
import math
2020-07-18 22:34:38 +03:00
2020-09-25 13:02:32 +03:00
type MyTime = time.Time
2020-07-18 22:34:38 +03:00
2020-09-16 23:21:02 +03:00
fn (mut t MyTime) century() int {
2020-12-25 18:19:00 +03:00
return int(1.0 + math.trunc(f64(t.year) * 0.009999794661191))
2020-09-16 23:21:02 +03:00
}
2020-07-18 22:34:38 +03:00
fn main() {
2020-12-06 00:54:41 +03:00
mut my_time := MyTime{
year: 2020
month: 12
day: 25
}
println(time.new_time(my_time).utc_string())
println('Century: $my_time.century()')
2020-07-18 22:34:38 +03:00
}
```
2020-06-26 20:36:40 +03:00
2020-07-23 19:55:54 +03:00
## Statements & expressions
2020-06-26 20:36:40 +03:00
### If
2019-11-21 04:36:55 +03:00
```v
a := 10
b := 20
if a < b {
2020-12-06 00:54:41 +03:00
println('$a < $b')
2019-11-21 04:36:55 +03:00
} else if a > b {
2020-12-06 00:54:41 +03:00
println('$a > $b')
2019-11-21 04:36:55 +03:00
} else {
2020-12-06 00:54:41 +03:00
println('$a == $b')
2019-11-21 04:36:55 +03:00
}
```
`if` statements are pretty straightforward and similar to most other languages.
2020-10-20 21:14:56 +03:00
Unlike other C-like languages,
there are no parentheses surrounding the condition and the braces are always required.
2019-11-21 04:36:55 +03:00
`if` can be used as an expression:
```v
num := 777
2020-12-06 00:54:41 +03:00
s := if num % 2 == 0 { 'even' } else { 'odd' }
println(s)
// "odd"
2019-11-21 04:36:55 +03:00
```
2020-11-27 23:24:35 +03:00
#### Type checks and casts
You can check the current type of a sum type using `is` and its negated form `!is` .
You can do it either in an `if` :
2021-01-10 22:21:37 +03:00
```v
2020-07-09 18:14:14 +03:00
struct Abc {
2021-01-10 22:21:37 +03:00
val string
2020-07-09 18:14:14 +03:00
}
2021-01-10 22:21:37 +03:00
2020-07-09 18:14:14 +03:00
struct Xyz {
2021-01-10 22:21:37 +03:00
foo string
2020-07-09 18:14:14 +03:00
}
2021-01-10 22:21:37 +03:00
2020-07-09 18:14:14 +03:00
type Alphabet = Abc | Xyz
x := Alphabet(Abc{'test'}) // sum type
if x is Abc {
2021-01-10 22:21:37 +03:00
// x is automatically casted to Abc and can be used here
println(x)
2020-07-09 18:14:14 +03:00
}
2020-11-27 23:24:35 +03:00
if x !is Abc {
2021-01-10 22:21:37 +03:00
println('Not Abc')
2020-11-27 23:24:35 +03:00
}
```
or using `match` :
```v oksyntax
match x {
2020-12-06 00:54:41 +03:00
Abc {
// x is automatically casted to Abc and can be used here
println(x)
}
Xyz {
// x is automatically casted to Xyz and can be used here
println(x)
}
2020-11-27 23:24:35 +03:00
}
2020-07-09 18:14:14 +03:00
```
2020-11-27 23:24:35 +03:00
This works also with struct fields:
```v
struct MyStruct {
2020-12-06 00:54:41 +03:00
x int
2020-11-27 23:24:35 +03:00
}
2020-12-06 00:54:41 +03:00
2020-11-27 23:24:35 +03:00
struct MyStruct2 {
2020-12-06 00:54:41 +03:00
y string
2020-11-27 23:24:35 +03:00
}
2020-11-03 03:04:14 +03:00
type MySumType = MyStruct | MyStruct2
2020-11-27 23:24:35 +03:00
struct Abc {
2020-12-06 00:54:41 +03:00
bar MySumType
2020-11-27 23:24:35 +03:00
}
x := Abc{
2020-12-06 00:54:41 +03:00
bar: MyStruct{123} // MyStruct will be converted to MySumType type automatically
2020-11-27 23:24:35 +03:00
}
if x.bar is MyStruct {
2020-12-06 00:54:41 +03:00
// x.bar is automatically casted
println(x.bar)
2020-11-27 23:24:35 +03:00
}
match x.bar {
2020-12-06 00:54:41 +03:00
MyStruct {
// x.bar is automatically casted
println(x.bar)
}
else {}
2020-11-27 23:24:35 +03:00
}
```
Mutable variables can change, and doing a cast would be unsafe.
2021-05-06 16:43:41 +03:00
However, sometimes it's useful to type cast despite mutability.
In such cases the developer must mark the expression with the `mut` keyword
to tell the compiler that they know what they're doing.
2020-11-27 23:24:35 +03:00
It works like this:
```v oksyntax
mut x := MySumType(MyStruct{123})
if mut x is MyStruct {
2021-01-04 22:53:28 +03:00
// x is casted to MyStruct even if it's mutable
2020-12-06 00:54:41 +03:00
// without the mut keyword that wouldn't work
println(x)
2020-11-27 23:24:35 +03:00
}
// same with match
match mut x {
2020-12-06 00:54:41 +03:00
MyStruct {
2021-10-18 21:03:27 +03:00
// x is casted to MyStruct even if it's mutable
2020-12-06 00:54:41 +03:00
// without the mut keyword that wouldn't work
println(x)
}
2020-07-09 18:14:14 +03:00
}
```
2020-06-26 20:36:40 +03:00
### In operator
2019-11-21 04:36:55 +03:00
`in` allows to check whether an array or a map contains an element.
2021-02-05 18:45:20 +03:00
To do the opposite, use `!in` .
2019-11-21 04:36:55 +03:00
2021-01-10 22:21:37 +03:00
```v
2019-11-21 04:36:55 +03:00
nums := [1, 2, 3]
println(1 in nums) // true
2021-02-05 18:45:20 +03:00
println(4 !in nums) // true
2021-08-04 12:44:41 +03:00
m := {
2021-01-10 22:21:37 +03:00
'one': 1
'two': 2
}
2019-11-21 04:36:55 +03:00
println('one' in m) // true
2021-02-05 18:45:20 +03:00
println('three' !in m) // true
2019-11-21 04:36:55 +03:00
```
2020-10-25 15:55:57 +03:00
It's also useful for writing boolean expressions that are clearer and more compact:
2019-11-21 04:36:55 +03:00
```v
2020-12-06 00:54:41 +03:00
enum Token {
plus
minus
div
mult
}
struct Parser {
token Token
2019-11-21 04:36:55 +03:00
}
2020-12-06 00:54:41 +03:00
parser := Parser{}
if parser.token == .plus || parser.token == .minus || parser.token == .div || parser.token == .mult {
// ...
}
2019-11-21 04:36:55 +03:00
if parser.token in [.plus, .minus, .div, .mult] {
2020-12-06 00:54:41 +03:00
// ...
2019-11-21 04:36:55 +03:00
}
```
2020-10-20 21:14:56 +03:00
V optimizes such expressions,
so both `if` statements above produce the same machine code and no arrays are created.
2019-11-21 04:36:55 +03:00
2020-06-26 20:36:40 +03:00
### For loop
2019-11-21 04:36:55 +03:00
2020-07-23 19:55:35 +03:00
V has only one looping keyword: `for` , with several forms.
2021-02-13 17:53:02 +03:00
#### `for`/`in`
2021-02-28 19:01:31 +03:00
This is the most common form. You can use it with an array, map or
2021-02-13 17:53:02 +03:00
numeric range.
##### Array `for`
2019-11-21 04:36:55 +03:00
2021-01-10 22:21:37 +03:00
```v
2019-11-21 04:36:55 +03:00
numbers := [1, 2, 3, 4, 5]
for num in numbers {
2021-01-10 22:21:37 +03:00
println(num)
2019-11-21 04:36:55 +03:00
}
names := ['Sam', 'Peter']
for i, name in names {
2021-01-10 22:21:37 +03:00
println('$i) $name')
// Output: 0) Sam
// 1) Peter
}
2019-11-21 04:36:55 +03:00
```
2020-07-23 19:55:35 +03:00
The `for value in arr` form is used for going through elements of an array.
If an index is required, an alternative form `for index, value in arr` can be used.
2019-11-21 04:36:55 +03:00
2020-10-20 21:14:56 +03:00
Note, that the value is read-only.
2021-03-11 15:53:58 +03:00
If you need to modify the array while looping, you need to declare the element as mutable:
2019-11-21 04:36:55 +03:00
2021-01-10 22:21:37 +03:00
```v
2020-06-30 15:29:55 +03:00
mut numbers := [0, 1, 2]
2021-03-11 15:53:58 +03:00
for mut num in numbers {
num++
2019-11-21 04:36:55 +03:00
}
2020-06-30 15:29:55 +03:00
println(numbers) // [1, 2, 3]
2019-11-21 04:36:55 +03:00
```
2020-06-30 15:29:55 +03:00
When an identifier is just a single underscore, it is ignored.
2019-11-21 04:36:55 +03:00
2021-07-01 12:30:43 +03:00
##### Custom iterators
Types that implement a `next` method returning an `Option` can be iterated
2021-07-09 23:53:25 +03:00
with a `for` loop.
2021-07-01 12:30:43 +03:00
```v
struct SquareIterator {
arr []int
mut:
idx int
}
fn (mut iter SquareIterator) next() ?int {
if iter.idx >= iter.arr.len {
return error('')
}
defer {
iter.idx++
}
return iter.arr[iter.idx] * iter.arr[iter.idx]
}
nums := [1, 2, 3, 4, 5]
iter := SquareIterator{
arr: nums
}
for squared in iter {
println(squared)
}
```
The code above prints:
```
1
4
9
16
25
```
2021-02-13 17:53:02 +03:00
##### Map `for`
2020-07-31 19:08:12 +03:00
2021-01-10 22:21:37 +03:00
```v
2021-08-04 12:44:41 +03:00
m := {
2021-01-10 22:21:37 +03:00
'one': 1
'two': 2
}
2020-07-31 19:08:12 +03:00
for key, value in m {
2021-01-10 22:21:37 +03:00
println('$key -> $value')
// Output: one -> 1
// two -> 2
}
2020-07-31 19:08:12 +03:00
```
2020-12-23 20:03:56 +03:00
Either key or value can be ignored by using a single underscore as the identifier.
2021-01-10 22:21:37 +03:00
```v
2021-08-04 12:44:41 +03:00
m := {
2021-01-10 22:21:37 +03:00
'one': 1
'two': 2
}
2020-07-31 19:08:12 +03:00
// iterate over keys
for key, _ in m {
2021-01-10 22:21:37 +03:00
println(key)
// Output: one
// two
}
2020-07-31 19:08:12 +03:00
// iterate over values
for _, value in m {
2021-01-10 22:21:37 +03:00
println(value)
// Output: 1
// 2
}
2020-07-31 19:08:12 +03:00
```
2021-02-13 17:53:02 +03:00
##### Range `for`
2020-07-23 19:55:35 +03:00
```v
// Prints '01234'
2020-12-06 00:54:41 +03:00
for i in 0 .. 5 {
print(i)
2020-07-23 19:55:35 +03:00
}
```
2020-08-12 20:14:13 +03:00
`low..high` means an *exclusive* range, which represents all values
2020-07-23 19:55:35 +03:00
from `low` up to *but not including* `high` .
#### Condition `for`
2021-01-10 22:21:37 +03:00
```v
2020-04-21 06:11:50 +03:00
mut sum := 0
mut i := 0
2019-11-21 04:36:55 +03:00
for i < = 100 {
2020-12-06 00:54:41 +03:00
sum += i
i++
2019-11-21 04:36:55 +03:00
}
println(sum) // "5050"
```
This form of the loop is similar to `while` loops in other languages.
The loop will stop iterating once the boolean condition evaluates to false.
Again, there are no parentheses surrounding the condition, and the braces are always required.
2020-07-23 19:55:35 +03:00
#### Bare `for`
2021-01-10 22:21:37 +03:00
```v
2020-04-21 06:11:50 +03:00
mut num := 0
2019-11-21 04:36:55 +03:00
for {
2020-12-06 00:54:41 +03:00
num += 2
if num >= 10 {
break
}
2019-11-21 04:36:55 +03:00
}
println(num) // "10"
```
2020-05-07 13:30:41 +03:00
The condition can be omitted, resulting in an infinite loop.
2019-11-21 04:36:55 +03:00
2020-07-23 19:55:35 +03:00
#### C `for`
2019-11-21 04:36:55 +03:00
```v
2020-07-23 19:55:35 +03:00
for i := 0; i < 10 ; i + = 2 {
2020-12-06 00:54:41 +03:00
// Don't print 6
if i == 6 {
continue
}
println(i)
2019-11-21 04:36:55 +03:00
}
```
Finally, there's the traditional C style `for` loop. It's safer than the `while` form
because with the latter it's easy to forget to update the counter and get
stuck in an infinite loop.
2020-04-21 06:11:50 +03:00
Here `i` doesn't need to be declared with `mut` since it's always going to be mutable by definition.
2019-11-21 04:36:55 +03:00
2020-11-20 16:11:56 +03:00
#### Labelled break & continue
`break` and `continue` control the innermost `for` loop by default.
2020-12-01 18:09:33 +03:00
You can also use `break` and `continue` followed by a label name to refer to an outer `for`
2020-11-20 16:11:56 +03:00
loop:
```v
2020-12-06 00:54:41 +03:00
outer: for i := 4; true; i++ {
2020-11-20 16:11:56 +03:00
println(i)
for {
2020-12-06 00:54:41 +03:00
if i < 7 {
continue outer
} else {
break outer
}
2020-11-20 16:11:56 +03:00
}
}
```
The label must immediately precede the outer loop.
The above code prints:
```
4
5
6
7
```
2020-06-26 20:36:40 +03:00
### Match
2019-11-21 04:36:55 +03:00
```v
os := 'windows'
print('V is running on ')
match os {
2020-12-06 00:54:41 +03:00
'darwin' { println('macOS.') }
'linux' { println('Linux.') }
else { println(os) }
2019-11-21 04:36:55 +03:00
}
2020-07-08 16:56:49 +03:00
```
A match statement is a shorter way to write a sequence of `if - else` statements.
When a matching branch is found, the following statement block will be run.
The else branch will be run when no other branches match.
2019-11-21 04:36:55 +03:00
2020-07-08 16:56:49 +03:00
```v
2020-01-16 17:29:00 +03:00
number := 2
2019-11-21 04:36:55 +03:00
s := match number {
2020-12-06 00:54:41 +03:00
1 { 'one' }
2 { 'two' }
else { 'many' }
2019-11-21 04:36:55 +03:00
}
```
2021-09-07 06:38:39 +03:00
A match statement can also to be used as an `if - else if - else` alternative:
```v
match true {
2 > 4 { println('if') }
3 == 4 { println('else if') }
2 == 2 { println('else if2') }
else { println('else') }
}
// 'else if2' should be printed
```
or as an `unless` alternative: [unless Ruby ](https://www.tutorialspoint.com/ruby/ruby_if_else.htm )
```v
match false {
2 > 4 { println('if') }
3 == 4 { println('else if') }
2 == 2 { println('else if2') }
else { println('else') }
}
// 'if' should be printed
```
2020-12-25 18:28:12 +03:00
A match expression returns the value of the final expression from the matching branch.
2019-11-21 04:36:55 +03:00
```v
enum Color {
2020-12-06 00:54:41 +03:00
red
blue
green
2019-11-21 04:36:55 +03:00
}
fn is_red_or_blue(c Color) bool {
2020-12-06 00:54:41 +03:00
return match c {
.red, .blue { true } // comma can be used to test multiple values
.green { false }
}
2019-11-21 04:36:55 +03:00
}
```
A match statement can also be used to branch on the variants of an `enum`
2020-07-08 16:56:49 +03:00
by using the shorthand `.variant_here` syntax. An `else` branch is not allowed
when all the branches are exhaustive.
2019-11-21 04:36:55 +03:00
2020-07-22 03:31:32 +03:00
```v
c := `v`
typ := match c {
2020-12-06 00:54:41 +03:00
`0` ...`9` { 'digit' }
`A` ...`Z` { 'uppercase' }
`a` ...`z` { 'lowercase' }
else { 'other' }
2020-07-22 03:31:32 +03:00
}
2020-12-06 00:54:41 +03:00
println(typ)
// 'lowercase'
2020-07-22 03:31:32 +03:00
```
You can also use ranges as `match` patterns. If the value falls within the range
of a branch, that branch will be executed.
Note that the ranges use `...` (three dots) rather than `..` (two dots). This is
because the range is *inclusive* of the last element, rather than exclusive
(as `..` ranges are). Using `..` in a match branch will throw an error.
2020-10-03 16:30:58 +03:00
Note: `match` as an expression is not usable in `for` loop and `if` statements.
2020-06-26 20:36:40 +03:00
### Defer
2020-10-20 21:14:56 +03:00
A defer statement defers the execution of a block of statements
until the surrounding function returns.
2020-06-26 20:36:40 +03:00
```v
2020-11-03 03:04:14 +03:00
import os
2020-06-26 20:36:40 +03:00
fn read_log() {
2020-12-06 00:54:41 +03:00
mut ok := false
2021-02-28 23:20:21 +03:00
mut f := os.open('log.txt') or { panic(err.msg) }
2020-12-06 00:54:41 +03:00
defer {
f.close()
}
// ...
if !ok {
// defer statement will be called here, the file will be closed
return
}
// ...
// defer statement will be called here, the file will be closed
2020-06-26 20:36:40 +03:00
}
```
2021-04-27 01:42:16 +03:00
If the function returns a value the `defer` block is executed *after* the return
2021-04-29 02:17:37 +03:00
expression is evaluated:
2021-04-27 01:42:16 +03:00
```v
import os
enum State {
normal
write_log
return_error
}
// write log file and return number of bytes written
fn write_log(s State) ?int {
mut f := os.create('log.txt') ?
defer {
f.close()
}
if s == .write_log {
// `f.close()` will be called after `f.write()` has been
// executed, but before `write_log()` finally returns the
// number of bytes written to `main()`
return f.writeln('This is a log file')
} else if s == .return_error {
// the file will be closed after the `error()` function
// has returned - so the error message will still report
// it as open
return error('nothing written; file open: $f.is_opened')
}
// the file will be closed here, too
return 0
}
fn main() {
n := write_log(.return_error) or {
println('Error: $err')
0
}
println('$n bytes written')
}
```
2019-11-21 04:36:55 +03:00
## Structs
2021-01-10 22:21:37 +03:00
```v
2019-11-21 04:36:55 +03:00
struct Point {
2021-01-10 22:21:37 +03:00
x int
y int
2019-11-21 04:36:55 +03:00
}
2020-07-08 17:02:35 +03:00
mut p := Point{
2021-01-10 22:21:37 +03:00
x: 10
y: 20
2019-11-21 04:36:55 +03:00
}
println(p.x) // Struct fields are accessed using a dot
2020-07-08 17:02:35 +03:00
// Alternative literal syntax for structs with 3 fields or fewer
p = Point{10, 20}
assert p.x == 10
2019-11-21 04:36:55 +03:00
```
2020-07-23 19:55:54 +03:00
### Heap structs
2020-05-16 16:19:48 +03:00
2019-11-21 04:36:55 +03:00
Structs are allocated on the stack. To allocate a struct on the heap
and get a reference to it, use the `&` prefix:
```v
2020-11-03 03:04:14 +03:00
struct Point {
2020-12-06 00:54:41 +03:00
x int
y int
2020-11-03 03:04:14 +03:00
}
2020-12-06 00:54:41 +03:00
2019-11-21 04:36:55 +03:00
p := & Point{10, 10}
// References have the same syntax for accessing fields
println(p.x)
```
2020-07-23 19:55:54 +03:00
The type of `p` is `&Point` . It's a [reference ](#references ) to `Point` .
2019-11-21 04:36:55 +03:00
References are similar to Go pointers and C++ references.
2020-07-23 19:55:54 +03:00
### Embedded structs
2020-05-16 16:17:27 +03:00
2020-05-16 16:19:48 +03:00
V doesn't allow subclassing, but it supports embedded structs:
2019-11-21 04:36:55 +03:00
2020-11-04 01:08:03 +03:00
```v
2020-11-12 15:50:37 +03:00
struct Widget {
mut:
x int
y int
}
2019-11-21 04:36:55 +03:00
struct Button {
2020-12-06 00:54:41 +03:00
Widget
2020-12-23 21:12:49 +03:00
title string
2019-11-21 04:36:55 +03:00
}
2020-12-06 00:54:41 +03:00
mut button := Button{
title: 'Click me'
}
2020-11-12 15:50:37 +03:00
button.x = 3
```
Without embedding we'd have to name the `Widget` field and do:
2019-11-21 04:36:55 +03:00
2020-11-12 15:50:37 +03:00
```v oksyntax
button.widget.x = 3
2019-11-21 04:36:55 +03:00
```
2020-07-23 19:55:54 +03:00
### Default field values
2020-05-16 16:19:48 +03:00
2020-05-16 16:17:27 +03:00
```v
struct Foo {
2021-01-04 00:54:13 +03:00
n int // n is 0 by default
2020-12-06 00:54:41 +03:00
s string // s is '' by default
2021-01-04 00:54:13 +03:00
a []int // a is `[]int{}` by default
2020-12-06 00:54:41 +03:00
pos int = -1 // custom default value
2020-05-16 16:17:27 +03:00
}
```
2020-05-16 16:19:48 +03:00
2020-10-20 21:14:56 +03:00
All struct fields are zeroed by default during the creation of the struct.
Array and map fields are allocated.
2020-05-17 17:01:02 +03:00
It's also possible to define custom default values.
2020-05-16 16:17:27 +03:00
2020-11-27 16:37:12 +03:00
### Required fields
```v
struct Foo {
2020-12-06 00:54:41 +03:00
n int [required]
2020-11-27 16:37:12 +03:00
}
```
You can mark a struct field with the `[required]` attribute, to tell V that
that field must be initialized when creating an instance of that struct.
This example will not compile, since the field `n` isn't explicitly initialized:
```v failcompile
_ = Foo{}
```
2020-05-17 17:11:48 +03:00
2020-07-08 17:02:35 +03:00
< a id = 'short-struct-initialization-syntax' / >
2020-05-17 17:11:48 +03:00
2020-07-23 01:37:37 +03:00
### Short struct literal syntax
```v
2020-12-06 00:54:41 +03:00
struct Point {
x int
y int
2020-11-03 03:04:14 +03:00
}
2020-07-23 01:37:37 +03:00
2020-12-06 00:54:41 +03:00
mut p := Point{
x: 10
y: 20
}
2021-08-04 12:44:41 +03:00
p = Point{
2020-12-06 00:54:41 +03:00
x: 30
y: 4
}
2020-07-23 01:37:37 +03:00
assert p.y == 4
2021-06-08 20:35:22 +03:00
//
// array: first element defines type of array
points := [Point{10, 20}, Point{20, 30}, Point{40, 50}]
println(points) // [Point{x: 10, y: 20}, Point{x: 20, y: 30}, Point{x: 40,y: 50}]
2020-07-23 01:37:37 +03:00
```
Omitting the struct name also works for returning a struct literal or passing one
as a function argument.
#### Trailing struct literal arguments
V doesn't have default function arguments or named arguments, for that trailing struct
literal syntax can be used instead:
2020-05-17 17:11:48 +03:00
```v
2021-10-26 10:53:08 +03:00
[params]
2020-05-17 17:11:48 +03:00
struct ButtonConfig {
2020-12-06 00:54:41 +03:00
text string
is_disabled bool
width int = 70
height int = 20
2020-05-17 17:11:48 +03:00
}
2020-05-17 17:15:02 +03:00
2020-12-01 18:09:33 +03:00
struct Button {
2020-12-06 00:54:41 +03:00
text string
width int
height int
2020-11-03 03:04:14 +03:00
}
2020-05-17 17:11:48 +03:00
fn new_button(c ButtonConfig) & Button {
2020-12-06 00:54:41 +03:00
return & Button{
width: c.width
height: c.height
text: c.text
}
2020-05-17 17:11:48 +03:00
}
2020-12-06 00:54:41 +03:00
button := new_button(text: 'Click me', width: 100)
2020-07-08 17:02:35 +03:00
// the height is unset, so it's the default value
assert button.height == 20
2020-05-17 17:11:48 +03:00
```
2020-07-23 01:37:37 +03:00
As you can see, both the struct name and braces can be omitted, instead of:
2020-05-17 17:15:02 +03:00
2021-02-05 20:50:28 +03:00
```v oksyntax nofmt
2020-05-17 17:15:02 +03:00
new_button(ButtonConfig{text:'Click me', width:100})
```
2020-07-23 01:37:37 +03:00
This only works for functions that take a struct for the last argument.
2020-05-17 17:15:02 +03:00
2021-10-26 10:53:08 +03:00
NB: the `[params]` tag is used to tell V, that the trailing struct parameter
2021-10-31 15:12:04 +03:00
can be omitted *entirely* , so that you can write `button := new_button()` .
2021-10-26 10:53:08 +03:00
Without it, you have to specify *at least* one of the field names, even if it
has its default value, otherwise the compiler will produce this error message,
when you call the function with no parameters:
`error: expected 1 arguments, but got 0` .
2020-06-26 20:36:40 +03:00
### Access modifiers
2019-11-21 04:36:55 +03:00
Struct fields are private and immutable by default (making structs immutable as well).
Their access modifiers can be changed with
2020-04-21 06:11:50 +03:00
`pub` and `mut` . In total, there are 5 possible options:
2019-11-21 04:36:55 +03:00
2021-02-05 20:50:28 +03:00
```v
2019-11-21 04:36:55 +03:00
struct Foo {
2021-02-05 20:50:28 +03:00
a int // private immutable (default)
2020-04-23 20:05:27 +03:00
mut:
2021-02-05 20:50:28 +03:00
b int // private mutable
c int // (you can list multiple fields with the same access modifier)
2019-11-21 04:36:55 +03:00
pub:
2021-02-05 20:50:28 +03:00
d int // public immutable (readonly)
2020-04-23 20:05:27 +03:00
pub mut:
2021-02-05 20:50:28 +03:00
e int // public, but mutable only in parent module
2019-11-21 04:36:55 +03:00
__global:
2021-02-05 20:50:28 +03:00
// (not recommended to use, that's why the 'global' keyword starts with __ )
f int // public and mutable both inside and outside parent module
}
2019-11-21 04:36:55 +03:00
```
For example, here's the `string` type defined in the `builtin` module:
2020-11-03 03:04:14 +03:00
```v ignore
2019-11-21 04:36:55 +03:00
struct string {
2021-04-12 15:58:26 +03:00
str & byte
2019-11-21 04:36:55 +03:00
pub:
len int
}
```
It's easy to see from this definition that `string` is an immutable type.
The byte pointer with the string data is not accessible outside `builtin` at all.
2020-05-07 13:30:41 +03:00
The `len` field is public, but immutable:
2020-11-03 03:04:14 +03:00
```v failcompile
2019-11-21 04:36:55 +03:00
fn main() {
2021-04-12 15:13:00 +03:00
str := 'hello'
len := str.len // OK
str.len++ // Compilation error
2019-11-21 04:36:55 +03:00
}
```
2020-10-20 21:14:56 +03:00
This means that defining public readonly fields is very easy in V,
no need in getters/setters or properties.
2020-05-16 16:36:19 +03:00
2021-02-24 21:27:48 +03:00
## Methods
2019-11-21 04:36:55 +03:00
2021-01-10 22:21:37 +03:00
```v
2019-11-21 04:36:55 +03:00
struct User {
2021-01-10 22:21:37 +03:00
age int
2019-11-21 04:36:55 +03:00
}
fn (u User) can_register() bool {
2021-01-10 22:21:37 +03:00
return u.age > 16
2019-11-21 04:36:55 +03:00
}
2021-01-10 22:21:37 +03:00
user := User{
age: 10
}
2019-11-21 04:36:55 +03:00
println(user.can_register()) // "false"
2021-01-10 22:21:37 +03:00
user2 := User{
age: 20
}
2019-11-21 04:36:55 +03:00
println(user2.can_register()) // "true"
```
2020-07-23 19:55:54 +03:00
V doesn't have classes, but you can define methods on types.
2019-11-21 04:36:55 +03:00
A method is a function with a special receiver argument.
The receiver appears in its own argument list between the `fn` keyword and the method name.
2021-02-24 21:27:48 +03:00
Methods must be in the same module as the receiver type.
2019-11-21 04:36:55 +03:00
In this example, the `can_register` method has a receiver of type `User` named `u` .
The convention is not to use receiver names like `self` or `this` ,
but a short, preferably one letter long, name.
2021-02-21 12:51:34 +03:00
## Unions
Just like structs, unions support embedding.
```v
struct Rgba32_Component {
r byte
g byte
b byte
a byte
}
union Rgba32 {
Rgba32_Component
value u32
}
clr1 := Rgba32{
value: 0x008811FF
}
clr2 := Rgba32{
2021-08-04 12:44:41 +03:00
Rgba32_Component: Rgba32_Component{
2021-02-21 12:51:34 +03:00
a: 128
}
}
sz := sizeof(Rgba32)
unsafe {
println('Size: ${sz}B,clr1.b: $clr1.b,clr2.b: $clr2.b')
}
```
Output: `Size: 4B, clr1.b: 136, clr2.b: 0`
Union member access must be performed in an `unsafe` block.
Note that the embedded struct arguments are not necessarily stored in the order listed.
2020-06-26 20:36:40 +03:00
## Functions 2
### Pure functions by default
2019-11-21 04:36:55 +03:00
2020-10-20 21:14:56 +03:00
V functions are pure by default, meaning that their return values are a function of their
arguments only, and their evaluation has no side effects (besides I/O).
2019-11-21 04:36:55 +03:00
2020-10-20 21:14:56 +03:00
This is achieved by a lack of global variables and all function arguments being
immutable by default, even when [references ](#references ) are passed.
2019-11-21 04:36:55 +03:00
2020-05-07 13:30:41 +03:00
V is not a purely functional language however.
2021-04-28 07:44:28 +03:00
There is a compiler flag to enable global variables (`-enable-globals`), but this is
2020-06-30 15:29:55 +03:00
intended for low-level applications like kernels and drivers.
### Mutable arguments
2020-05-07 13:30:41 +03:00
It is possible to modify function arguments by using the keyword `mut` :
2019-11-21 04:36:55 +03:00
2021-02-05 20:50:28 +03:00
```v
2019-11-21 04:36:55 +03:00
struct User {
2020-11-03 03:04:14 +03:00
name string
2020-04-23 20:05:27 +03:00
mut:
2021-02-05 20:50:28 +03:00
is_registered bool
2019-11-21 04:36:55 +03:00
}
2020-04-21 06:11:50 +03:00
fn (mut u User) register() {
2021-02-05 20:50:28 +03:00
u.is_registered = true
2019-11-21 04:36:55 +03:00
}
2020-04-21 06:11:50 +03:00
mut user := User{}
2019-11-21 04:36:55 +03:00
println(user.is_registered) // "false"
user.register()
println(user.is_registered) // "true"
```
In this example, the receiver (which is simply the first argument) is marked as mutable,
so `register()` can change the user object. The same works with non-receiver arguments:
```v
2020-04-21 06:11:50 +03:00
fn multiply_by_2(mut arr []int) {
2020-12-06 00:54:41 +03:00
for i in 0 .. arr.len {
arr[i] *= 2
}
2019-11-21 04:36:55 +03:00
}
2020-04-21 06:11:50 +03:00
mut nums := [1, 2, 3]
multiply_by_2(mut nums)
2020-12-06 00:54:41 +03:00
println(nums)
// "[2, 4, 6]"
2019-11-21 04:36:55 +03:00
```
2020-04-21 06:11:50 +03:00
Note, that you have to add `mut` before `nums` when calling this function. This makes
2019-11-21 04:36:55 +03:00
it clear that the function being called will modify the value.
2021-10-31 12:32:31 +03:00
It is preferable to return values instead of modifying arguments,
e.g. `user = register(user)` (or `user.register()` ) instead of `register(mut user)` .
2019-11-21 04:36:55 +03:00
Modifying arguments should only be done in performance-critical parts of your application
to reduce allocations and copying.
2020-10-20 21:14:56 +03:00
For this reason V doesn't allow the modification of arguments with primitive types (e.g. integers).
Only more complex types such as arrays and maps may be modified.
2019-11-21 04:36:55 +03:00
2021-02-07 05:43:51 +03:00
#### Struct update syntax
2019-11-21 04:36:55 +03:00
V makes it easy to return a modified version of an object:
```v
2020-12-06 00:54:41 +03:00
struct User {
name string
age int
is_registered bool
}
2019-11-21 04:36:55 +03:00
fn register(u User) User {
2021-08-04 12:44:41 +03:00
return User{
2021-02-07 05:43:51 +03:00
...u
2020-12-06 00:54:41 +03:00
is_registered: true
}
2019-11-21 04:36:55 +03:00
}
2020-12-06 00:54:41 +03:00
mut user := User{
name: 'abc'
age: 23
}
2019-11-21 04:36:55 +03:00
user = register(user)
2020-11-03 03:04:14 +03:00
println(user)
2019-11-21 04:36:55 +03:00
```
2021-02-24 21:27:48 +03:00
### Variable number of arguments
```v
fn sum(a ...int) int {
mut total := 0
for x in a {
total += x
}
return total
}
println(sum()) // 0
println(sum(1)) // 1
println(sum(2, 3)) // 5
// using array decomposition
a := [2, 3, 4]
println(sum(...a)) // < -- using prefix . . . here . output: 9
b := [5, 6, 7]
println(sum(...b)) // output: 18
```
2021-06-13 21:17:57 +03:00
### Anonymous & higher order functions
2019-11-21 04:36:55 +03:00
```v
fn sqr(n int) int {
2020-12-06 00:54:41 +03:00
return n * n
2019-11-21 04:36:55 +03:00
}
2021-02-05 10:05:13 +03:00
fn cube(n int) int {
return n * n * n
}
2020-12-06 00:54:41 +03:00
fn run(value int, op fn (int) int) int {
return op(value)
2019-11-21 04:36:55 +03:00
}
2020-12-06 00:54:41 +03:00
fn main() {
2021-02-05 10:05:13 +03:00
// Functions can be passed to other functions
2020-12-06 00:54:41 +03:00
println(run(5, sqr)) // "25"
// Anonymous functions can be declared inside other functions:
double_fn := fn (n int) int {
return n + n
}
println(run(5, double_fn)) // "10"
// Functions can be passed around without assigning them to variables:
res := run(5, fn (n int) int {
return n + n
})
2021-02-12 22:10:54 +03:00
println(res) // "10"
2021-02-05 10:05:13 +03:00
// You can even have an array/map of functions:
fns := [sqr, cube]
2021-02-12 22:10:54 +03:00
println(fns[0](10)) // "100"
2021-08-04 12:44:41 +03:00
fns_map := {
2021-02-05 10:30:18 +03:00
'sqr': sqr
'cube': cube
}
2021-02-12 22:10:54 +03:00
println(fns_map['cube'](2)) // "8"
2019-11-21 04:36:55 +03:00
}
```
2021-09-30 12:43:26 +03:00
### Closures
2021-08-11 14:01:09 +03:00
V supports closures too.
This means that anonymous functions can inherit variables from the scope they were created in.
They must do so explicitly by listing all variables that are inherited.
> Warning: currently works on Unix-based, x64 architectures only.
Some work is in progress to make closures work on Windows, then other architectures.
2021-08-26 11:11:48 +03:00
```v oksyntax
2021-08-11 14:01:09 +03:00
my_int := 1
my_closure := fn [my_int] () {
println(my_int)
}
my_closure() // prints 1
```
Inherited variables are copied when the anonymous function is created.
This means that if the original variable is modified after the creation of the function,
the modification won't be reflected in the function.
2021-08-26 11:11:48 +03:00
```v oksyntax
2021-08-11 14:01:09 +03:00
mut i := 1
func := fn [i] () int {
return i
}
println(func() == 1) // true
i = 123
println(func() == 1) // still true
```
However, the variable can be modified inside the anonymous function.
The change won't be reflected outside, but will be in the later function calls.
2021-08-26 11:11:48 +03:00
```v oksyntax
2021-08-11 14:01:09 +03:00
fn new_counter() fn () int {
mut i := 0
return fn [mut i] () int {
i++
return i
}
}
c := new_counter()
println(c()) // 1
println(c()) // 2
println(c()) // 3
```
If you need the value to be modified outside the function, use a reference.
**Warning**: _you need to make sure the reference is always valid,
otherwise this can result in undefined behavior._
2021-08-26 11:11:48 +03:00
```v oksyntax
2021-08-11 14:01:09 +03:00
mut i := 0
mut ref := & i
print_counter := fn [ref] () {
println(*ref)
}
print_counter() // 0
i = 10
print_counter() // 10
```
2019-11-21 04:36:55 +03:00
## References
```v
2020-12-23 01:43:48 +03:00
struct Foo {}
2020-12-06 00:54:41 +03:00
2019-11-21 04:36:55 +03:00
fn (foo Foo) bar_method() {
2020-12-06 00:54:41 +03:00
// ...
2019-11-21 04:36:55 +03:00
}
fn bar_function(foo Foo) {
2020-12-06 00:54:41 +03:00
// ...
2019-11-21 04:36:55 +03:00
}
```
2020-05-07 13:30:41 +03:00
If a function argument is immutable (like `foo` in the examples above)
2020-10-25 15:55:57 +03:00
V can pass it either by value or by reference. The compiler will decide,
2020-05-07 13:30:41 +03:00
and the developer doesn't need to think about it.
2019-11-21 04:36:55 +03:00
You no longer need to remember whether you should pass the struct by value
or by reference.
2020-05-07 13:30:41 +03:00
You can ensure that the struct is always passed by reference by
2019-11-21 04:36:55 +03:00
adding `&` :
```v
2020-12-06 00:54:41 +03:00
struct Foo {
abc int
}
2020-11-03 03:04:14 +03:00
2019-11-21 04:36:55 +03:00
fn (foo & Foo) bar() {
2020-12-06 00:54:41 +03:00
println(foo.abc)
2019-11-21 04:36:55 +03:00
}
```
`foo` is still immutable and can't be changed. For that,
2020-10-25 15:55:57 +03:00
`(mut foo Foo)` must be used.
2019-11-21 04:36:55 +03:00
2020-05-07 13:30:41 +03:00
In general, V's references are similar to Go pointers and C++ references.
2020-11-03 03:04:14 +03:00
For example, a generic tree structure definition would look like this:
2019-11-21 04:36:55 +03:00
2021-06-25 12:11:29 +03:00
```v
2019-11-21 04:36:55 +03:00
struct Node< T > {
2021-06-25 12:11:29 +03:00
val T
left & Node< T >
right & Node< T >
2019-11-21 04:36:55 +03:00
}
```
2021-09-10 22:53:05 +03:00
To dereference a reference, use the `*` operator, just like in C.
2019-11-21 04:36:55 +03:00
## Constants
2021-02-13 17:54:42 +03:00
```v
2019-11-21 04:36:55 +03:00
const (
2020-12-06 00:54:41 +03:00
pi = 3.14
world = '世界'
2019-11-21 04:36:55 +03:00
)
2021-01-25 14:08:43 +03:00
println(pi)
println(world)
2019-11-21 04:36:55 +03:00
```
Constants are declared with `const` . They can only be defined
at the module level (outside of functions).
2021-02-28 19:01:31 +03:00
Constant values can never be changed. You can also declare a single
2021-02-13 17:54:42 +03:00
constant separately:
2019-11-21 04:36:55 +03:00
2021-02-13 17:54:42 +03:00
```v
const e = 2.71828
```
2019-11-21 04:36:55 +03:00
V constants are more flexible than in most languages. You can assign more complex values:
```v
struct Color {
2020-12-06 00:54:41 +03:00
r int
g int
b int
2019-11-21 04:36:55 +03:00
}
2020-12-06 00:54:41 +03:00
fn rgb(r int, g int, b int) Color {
return Color{
r: r
g: g
b: b
}
}
2019-11-21 04:36:55 +03:00
const (
2020-12-06 00:54:41 +03:00
numbers = [1, 2, 3]
red = Color{
r: 255
g: 0
b: 0
}
2021-02-13 17:54:42 +03:00
// evaluate function call at compile-time*
2021-04-29 02:17:37 +03:00
blue = rgb(0, 0, 255)
2019-11-21 04:36:55 +03:00
)
2021-01-25 14:08:43 +03:00
println(numbers)
println(red)
println(blue)
2019-11-21 04:36:55 +03:00
```
2021-02-13 17:54:42 +03:00
\* WIP - for now function calls are evaluated at program start-up
2019-11-21 04:36:55 +03:00
2021-02-13 17:54:42 +03:00
Global variables are not normally allowed, so this can be really useful.
2021-06-15 04:45:27 +03:00
**Modules**
Constants can be made public with `pub const` :
```v oksyntax
module mymodule
pub const golden_ratio = 1.61803
fn calc() {
println(mymodule.golden_ratio)
}
```
The `pub` keyword is only allowed before the `const` keyword and cannot be used inside
a `const ( )` block.
Outside from module main all constants need to be prefixed with the module name.
2021-02-13 17:54:42 +03:00
### Required module prefix
2019-11-21 04:36:55 +03:00
2021-01-24 12:23:52 +03:00
When naming constants, `snake_case` must be used. In order to distinguish consts
from local variables, the full path to consts must be specified. For example,
2021-01-25 14:08:43 +03:00
to access the PI const, full `math.pi` name must be used both outside the `math`
2021-02-01 16:50:41 +03:00
module, and inside it. That restriction is relaxed only for the `main` module
2021-02-13 17:54:42 +03:00
(the one containing your `fn main()` ), where you can use the unqualified name of
constants defined there, i.e. `numbers` , rather than `main.numbers` .
2021-01-24 12:23:52 +03:00
vfmt takes care of this rule, so you can type `println(pi)` inside the `math` module,
2021-02-05 20:50:28 +03:00
and vfmt will automatically update it to `println(math.pi)` .
2021-01-24 12:23:52 +03:00
2020-05-21 17:34:57 +03:00
<!--
2019-11-21 04:36:55 +03:00
Many people prefer all caps consts: `TOP_CITIES` . This wouldn't work
well in V, because consts are a lot more powerful than in other languages.
They can represent complex structures, and this is used quite often since there
are no globals:
2021-02-05 20:50:28 +03:00
```v oksyntax
println('Top cities: ${top_cities.filter(.usa)}')
2019-11-21 04:36:55 +03:00
```
2021-02-13 17:54:42 +03:00
-->
2019-11-21 04:36:55 +03:00
2020-12-26 23:13:07 +03:00
## Builtin functions
2019-11-21 04:36:55 +03:00
2020-12-26 23:13:07 +03:00
Some functions are builtin like `println` . Here is the complete list:
```v ignore
fn print(s string) // print anything on sdtout
fn println(s string) // print anything and a newline on sdtout
fn eprint(s string) // same as print(), but use stderr
fn eprintln(s string) // same as println(), but use stderr
fn exit(code int) // terminate the program with a custom error code
fn panic(s string) // print a message and backtraces on stderr, and terminate the program with error code 1
fn print_backtrace() // print backtraces on stderr
```
`println` is a simple yet powerful builtin function, that can print anything:
2019-11-21 04:36:55 +03:00
strings, numbers, arrays, maps, structs.
2021-02-05 20:50:28 +03:00
```v
struct User {
name string
age int
}
2019-11-21 04:36:55 +03:00
println(1) // "1"
println('hi') // "hi"
2021-02-05 20:50:28 +03:00
println([1, 2, 3]) // "[1, 2, 3]"
println(User{ name: 'Bob', age: 20 }) // "User{name:'Bob', age:20}"
2019-11-21 04:36:55 +03:00
```
2021-02-13 17:57:51 +03:00
< a id = 'custom-print-of-types' / >
## Printing custom types
2020-12-28 21:29:58 +03:00
2019-11-21 04:36:55 +03:00
If you want to define a custom print value for your type, simply define a
2020-06-30 15:29:55 +03:00
`.str() string` method:
```v
struct Color {
2020-12-06 00:54:41 +03:00
r int
g int
b int
2020-06-30 15:29:55 +03:00
}
2020-12-06 00:54:41 +03:00
pub fn (c Color) str() string {
return '{$c.r, $c.g, $c.b}'
}
2020-06-30 15:29:55 +03:00
2020-12-06 00:54:41 +03:00
red := Color{
r: 255
g: 0
b: 0
}
2020-06-30 15:29:55 +03:00
println(red)
```
2019-11-21 04:36:55 +03:00
## Modules
2020-10-11 21:36:41 +03:00
Every file in the root of a folder is part of the same module.
2020-10-25 15:55:57 +03:00
Simple programs don't need to specify module name, in which case it defaults to 'main'.
2020-10-11 21:36:41 +03:00
2019-11-21 04:36:55 +03:00
V is a very modular language. Creating reusable modules is encouraged and is
2020-10-25 15:55:57 +03:00
quite easy to do.
2020-09-25 21:34:58 +03:00
To create a new module, create a directory with your module's name containing
2019-11-21 04:36:55 +03:00
.v files with code:
2020-11-03 03:04:14 +03:00
```shell
2019-11-21 04:36:55 +03:00
cd ~/code/modules
mkdir mymodule
2020-09-21 17:31:11 +03:00
vim mymodule/myfile.v
2020-09-25 21:34:58 +03:00
```
2020-11-03 03:04:14 +03:00
```v failcompile
2020-09-21 17:31:11 +03:00
// myfile.v
2019-11-21 04:36:55 +03:00
module mymodule
// To export a function we have to use `pub`
pub fn say_hi() {
2021-04-12 15:13:00 +03:00
println('hello from mymodule!')
2019-11-21 04:36:55 +03:00
}
```
2020-09-25 21:34:58 +03:00
You can now use `mymodule` in your code:
2019-11-21 04:36:55 +03:00
2020-11-03 03:04:14 +03:00
```v failcompile
2019-11-21 04:36:55 +03:00
import mymodule
fn main() {
2021-04-12 15:13:00 +03:00
mymodule.say_hi()
2019-11-21 04:36:55 +03:00
}
```
2020-09-25 21:34:58 +03:00
* Module names should be short, under 10 characters.
2020-12-27 13:23:43 +03:00
* Module names must use `snake_case` .
2020-09-25 21:34:58 +03:00
* Circular imports are not allowed.
* You can have as many .v files in a module as you want.
* You can create modules anywhere.
* All modules are compiled statically into a single executable.
2019-11-21 04:36:55 +03:00
2020-09-25 21:34:58 +03:00
### `init` functions
2019-11-21 04:36:55 +03:00
2020-10-15 13:32:28 +03:00
If you want a module to automatically call some setup/initialization code when it is imported,
2020-09-25 21:34:58 +03:00
you can use a module `init` function:
2019-11-21 04:36:55 +03:00
```v
2020-07-11 22:20:28 +03:00
fn init() {
2020-12-06 00:54:41 +03:00
// your setup code here ...
2019-11-21 04:36:55 +03:00
}
```
2020-10-15 13:32:28 +03:00
The `init` function cannot be public - it will be called automatically. This feature is
2020-09-25 21:34:58 +03:00
particularly useful for initializing a C library.
2019-11-21 04:36:55 +03:00
2021-06-22 12:39:57 +03:00
## Type Declarations
2020-06-26 20:36:40 +03:00
### Interfaces
2019-11-21 04:36:55 +03:00
```v
2020-09-30 08:42:23 +03:00
struct Dog {
2020-12-06 00:54:41 +03:00
breed string
2020-09-30 08:42:23 +03:00
}
2021-01-23 09:57:17 +03:00
struct Cat {
breed string
}
2019-11-21 04:36:55 +03:00
fn (d Dog) speak() string {
2020-12-06 00:54:41 +03:00
return 'woof'
2019-11-21 04:36:55 +03:00
}
fn (c Cat) speak() string {
2020-12-06 00:54:41 +03:00
return 'meow'
2019-11-21 04:36:55 +03:00
}
2021-01-23 09:57:17 +03:00
// unlike Go and like TypeScript, V's interfaces can define fields, not just methods.
2019-11-21 04:36:55 +03:00
interface Speaker {
2021-01-23 09:57:17 +03:00
breed string
2020-12-06 00:54:41 +03:00
speak() string
2019-11-21 04:36:55 +03:00
}
2020-09-30 08:42:23 +03:00
dog := Dog{'Leonberger'}
2021-01-23 09:57:17 +03:00
cat := Cat{'Siamese'}
2021-01-31 17:28:02 +03:00
2020-11-12 22:10:09 +03:00
mut arr := []Speaker{}
arr < < dog
arr < < cat
for item in arr {
2021-01-31 17:28:02 +03:00
println('a $item.breed says: $item.speak()')
2020-11-12 22:10:09 +03:00
}
2019-11-21 04:36:55 +03:00
```
2021-10-18 14:24:09 +03:00
#### Implement an interface
2021-01-23 09:57:17 +03:00
A type implements an interface by implementing its methods and fields.
2019-11-21 04:36:55 +03:00
There is no explicit declaration of intent, no "implements" keyword.
2021-10-18 14:24:09 +03:00
An interface can have a `mut:` section. Implementing types will need
to have a `mut` receiver, for methods declared in the `mut:` section
of an interface.
```v
module main
pub interface Foo {
write(string) string
}
// => the method signature of a type, implementing interface Foo should be:
// `pub fn (s Type) write(a string) string`
pub interface Bar {
mut:
write(string) string
}
// => the method signature of a type, implementing interface Bar should be:
// `pub fn (mut s Type) write(a string) string`
struct MyStruct {}
// MyStruct implements the interface Foo, but *not* interface Bar
pub fn (s MyStruct) write(a string) string {
return a
}
fn main() {
s1 := MyStruct{}
fn1(s1)
// fn2(s1) -> compile error, since MyStruct does not implement Bar
}
fn fn1(s Foo) {
println(s.write('Foo'))
}
// fn fn2(s Bar) { // does not match
// println(s.write('Foo'))
// }
```
2021-01-31 17:28:02 +03:00
#### Casting an interface
2020-11-12 22:10:09 +03:00
We can test the underlying type of an interface using dynamic cast operators:
```v oksyntax
2021-01-31 17:28:02 +03:00
interface Something {}
fn announce(s Something) {
2020-12-06 00:54:41 +03:00
if s is Dog {
2021-01-23 09:57:17 +03:00
println('a $s.breed dog') // `s` is automatically cast to `Dog` (smart cast)
2020-12-06 00:54:41 +03:00
} else if s is Cat {
2021-01-23 09:57:17 +03:00
println('a $s.breed cat')
2020-12-06 00:54:41 +03:00
} else {
println('something else')
}
2020-11-12 22:10:09 +03:00
}
```
For more information, see [Dynamic casts ](#dynamic-casts ).
2021-01-31 17:28:02 +03:00
#### Interface method definitions
2021-01-31 05:39:46 +03:00
Also unlike Go, an interface may implement a method.
These methods are not implemented by structs which implement that interface.
When a struct is wrapped in an interface that has implemented a method
with the same name as one implemented by this struct, only the method
implemented on the interface is called.
```v
struct Cat {}
fn (c Cat) speak() string {
return 'meow!'
}
2021-01-31 17:28:02 +03:00
interface Adoptable {}
2021-01-31 05:39:46 +03:00
fn (a Adoptable) speak() string {
return 'adopt me!'
}
fn new_adoptable() Adoptable {
return Cat{}
}
fn main() {
2021-01-31 17:28:02 +03:00
cat := Cat{}
assert cat.speak() == 'meow!'
a := new_adoptable()
assert a.speak() == 'adopt me!'
if a is Cat {
println(a.speak()) // meow!
}
2021-01-31 05:39:46 +03:00
}
```
2021-10-18 14:24:09 +03:00
#### Embedded interface
Interfaces support embedding, just like structs:
```v
pub interface Reader {
mut:
read(mut buf []byte) ?int
}
pub interface Writer {
mut:
write(buf []byte) ?int
}
// ReaderWriter embeds both Reader and Writer.
// The effect is the same as copy/pasting all of the
// Reader and all of the Writer methods/fields into
// ReaderWriter.
pub interface ReaderWriter {
Reader
Writer
}
```
2021-06-22 12:39:57 +03:00
### Function Types
You can use type aliases for naming specific function signatures - for
example:
```v
type Filter = fn (string) string
```
2021-06-25 12:11:29 +03:00
This works like any other type - for example, a function can accept an
2021-06-22 12:39:57 +03:00
argument of a function type:
```v
type Filter = fn (string) string
fn filter(s string, f Filter) string {
return f(s)
}
```
2021-06-25 12:11:29 +03:00
V has duck-typing, so functions don't need to declare compatibility with
2021-06-22 12:39:57 +03:00
a function type - they just have to be compatible:
```v
fn uppercase(s string) string {
return s.to_upper()
}
// now `uppercase` can be used everywhere where Filter is expected
```
Compatible functions can also be explicitly cast to a function type:
```v oksyntax
my_filter := Filter(uppercase)
```
The cast here is purely informational - again, duck-typing means that the
resulting type is the same without an explicit cast:
```v oksyntax
my_filter := uppercase
```
You can pass the assigned function as an argument:
```v oksyntax
println(filter('Hello world', my_filter)) // prints `HELLO WORLD`
```
And you could of course have passed it directly as well, without using a
local variable:
```v oksyntax
println(filter('Hello world', uppercase))
```
And this works with anonymous functions as well:
```v oksyntax
println(filter('Hello world', fn (s string) string {
return s.to_upper()
}))
```
2021-06-25 12:11:29 +03:00
You can see the complete
2021-06-22 12:39:57 +03:00
[example here ](https://github.com/vlang/v/tree/master/examples/function_types.v ).
2020-06-26 20:36:40 +03:00
### Enums
2019-11-21 04:36:55 +03:00
2021-01-10 22:21:37 +03:00
```v
2019-11-21 04:36:55 +03:00
enum Color {
2021-01-10 22:21:37 +03:00
red
green
blue
2019-11-21 04:36:55 +03:00
}
2020-04-21 06:11:50 +03:00
mut color := Color.red
2019-11-21 04:36:55 +03:00
// V knows that `color` is a `Color` . No need to use `color = Color.green` here.
color = .green
2020-08-24 12:14:35 +03:00
println(color) // "green"
match color {
2021-01-10 22:21:37 +03:00
.red { println('the color was red') }
.green { println('the color was green') }
.blue { println('the color was blue') }
2020-08-24 12:14:35 +03:00
}
2019-11-21 04:36:55 +03:00
```
2020-10-20 21:14:56 +03:00
Enum match must be exhaustive or have an `else` branch.
This ensures that if a new enum field is added, it's handled everywhere in the code.
2020-08-24 12:14:35 +03:00
2021-01-29 22:09:24 +03:00
Enum fields cannot re-use reserved keywords. However, reserved keywords may be escaped
with an @.
```v
enum Color {
@none
red
green
blue
}
color := Color.@none
println(color)
```
2021-02-21 12:54:13 +03:00
Integers may be assigned to enum fields.
```v
enum Grocery {
apple
orange = 5
pear
}
g1 := int(Grocery.apple)
g2 := int(Grocery.orange)
g3 := int(Grocery.pear)
println('Grocery IDs: $g1, $g2, $g3')
```
Output: `Grocery IDs: 0, 5, 6` .
2021-10-31 15:12:04 +03:00
Operations are not allowed on enum variables; they must be explicitly cast to `int` .
2021-02-21 12:54:13 +03:00
2020-06-26 20:36:40 +03:00
### Sum types
2020-05-05 16:42:53 +03:00
2020-07-06 19:09:38 +03:00
A sum type instance can hold a value of several different types. Use the `type`
keyword to declare a sum type:
2020-05-05 16:42:53 +03:00
2020-07-06 19:09:38 +03:00
```v
2020-12-23 01:43:48 +03:00
struct Moon {}
2020-05-05 16:42:53 +03:00
2020-12-23 01:43:48 +03:00
struct Mars {}
2020-12-06 00:54:41 +03:00
2020-12-23 01:43:48 +03:00
struct Venus {}
2020-12-06 00:54:41 +03:00
type World = Mars | Moon | Venus
2020-05-05 16:42:53 +03:00
2020-07-06 19:09:38 +03:00
sum := World(Moon{})
2020-11-14 14:59:03 +03:00
assert sum.type_name() == 'Moon'
2020-11-03 03:04:14 +03:00
println(sum)
2020-05-05 16:42:53 +03:00
```
2020-12-01 18:09:33 +03:00
The built-in method `type_name` returns the name of the currently held
2020-11-14 14:59:03 +03:00
type.
2020-05-05 16:42:53 +03:00
2021-01-17 18:27:22 +03:00
With sum types you could build recursive structures and write concise but powerful code on them.
```v
// V's binary tree
struct Empty {}
struct Node {
value f64
left Tree
right Tree
}
type Tree = Empty | Node
// sum up all node values
fn sum(tree Tree) f64 {
return match tree {
2021-05-27 20:13:02 +03:00
Empty { 0 }
2021-01-17 18:27:22 +03:00
Node { tree.value + sum(tree.left) + sum(tree.right) }
}
}
fn main() {
left := Node{0.2, Empty{}, Empty{}}
right := Node{0.3, Empty{}, Node{0.4, Empty{}, Empty{}}}
tree := Node{0.5, left, right}
println(sum(tree)) // 0.2 + 0.3 + 0.4 + 0.5 = 1.4
}
```
2021-06-28 16:25:02 +03:00
Enums can have methods, just like structs
```v
enum Cycle {
one
two
three
}
fn (c Cycle) next() Cycle {
match c {
.one {
return .two
}
.two {
return .three
}
.three {
return .one
}
}
}
mut c := Cycle.one
for _ in 0 .. 10 {
println(c)
c = c.next()
}
```
Output:
```
one
two
three
one
two
three
one
two
three
one
```
2020-11-12 22:10:09 +03:00
#### Dynamic casts
2020-07-06 19:09:38 +03:00
To check whether a sum type instance holds a certain type, use `sum is Type` .
2020-07-14 20:24:59 +03:00
To cast a sum type to one of its variants you can use `sum as Type` :
2020-05-05 16:42:53 +03:00
```v
2020-12-23 01:43:48 +03:00
struct Moon {}
2020-11-12 22:13:37 +03:00
2020-12-23 01:43:48 +03:00
struct Mars {}
2020-07-06 19:09:38 +03:00
2020-12-23 01:43:48 +03:00
struct Venus {}
2020-11-12 22:13:37 +03:00
2020-12-06 00:54:41 +03:00
type World = Mars | Moon | Venus
2020-07-06 19:09:38 +03:00
2020-12-06 00:54:41 +03:00
fn (m Mars) dust_storm() bool {
return true
}
fn main() {
mut w := World(Moon{})
assert w is Moon
w = Mars{}
// use `as` to access the Mars instance
mars := w as Mars
if mars.dust_storm() {
println('bad weather!')
}
2020-07-06 19:09:38 +03:00
}
2020-05-05 16:42:53 +03:00
```
2020-11-12 22:13:37 +03:00
`as` will panic if `w` doesn't hold a `Mars` instance.
A safer way is to use a smart cast.
#### Smart casting
```v oksyntax
if w is Mars {
2020-12-06 00:54:41 +03:00
assert typeof(w).name == 'Mars'
if w.dust_storm() {
println('bad weather!')
}
2020-11-12 22:13:37 +03:00
}
```
2020-12-01 18:09:33 +03:00
`w` has type `Mars` inside the body of the `if` statement. This is
2021-01-05 13:57:12 +03:00
known as *flow-sensitive typing* .
If `w` is a mutable identifier, it would be unsafe if the compiler smart casts it without a warning.
That's why you have to declare a `mut` before the `is` expression:
2020-11-12 22:13:37 +03:00
2020-11-27 16:28:40 +03:00
```v ignore
2021-01-05 13:57:12 +03:00
if mut w is Mars {
2021-02-05 20:50:28 +03:00
assert typeof(w).name == 'Mars'
if w.dust_storm() {
println('bad weather!')
}
2020-11-12 22:13:37 +03:00
}
```
2021-01-05 13:57:12 +03:00
Otherwise `w` would keep its original type.
> This works for both, simple variables and complex expressions like `user.name`
2020-11-12 22:13:37 +03:00
#### Matching sum types
2020-07-14 20:24:59 +03:00
2020-07-06 19:09:38 +03:00
You can also use `match` to determine the variant:
2020-06-18 18:37:41 +03:00
```v
2020-12-23 01:43:48 +03:00
struct Moon {}
2020-11-12 22:13:37 +03:00
2020-12-23 01:43:48 +03:00
struct Mars {}
2020-12-06 00:54:41 +03:00
2020-12-23 01:43:48 +03:00
struct Venus {}
2020-12-06 00:54:41 +03:00
type World = Mars | Moon | Venus
2020-11-12 22:13:37 +03:00
2020-12-06 00:54:41 +03:00
fn open_parachutes(n int) {
println(n)
}
2020-11-12 22:13:37 +03:00
2020-07-06 19:09:38 +03:00
fn land(w World) {
2020-12-06 00:54:41 +03:00
match w {
Moon {} // no atmosphere
Mars {
// light atmosphere
open_parachutes(3)
}
Venus {
// heavy atmosphere
open_parachutes(1)
}
}
2020-07-06 19:09:38 +03:00
}
2020-06-18 18:37:41 +03:00
```
2020-07-06 19:09:38 +03:00
`match` must have a pattern for each variant or have an `else` branch.
2020-11-27 16:28:40 +03:00
```v ignore
2020-11-03 03:04:14 +03:00
struct Moon {}
struct Mars {}
struct Venus {}
type World = Moon | Mars | Venus
fn (m Moon) moon_walk() {}
fn (m Mars) shiver() {}
fn (v Venus) sweat() {}
2020-07-06 19:09:38 +03:00
fn pass_time(w World) {
match w {
2020-08-24 12:14:35 +03:00
// using the shadowed match variable, in this case `w` (smart cast)
2020-07-06 19:09:38 +03:00
Moon { w.moon_walk() }
Mars { w.shiver() }
else {}
}
}
2020-06-18 18:37:41 +03:00
```
2021-03-26 20:50:29 +03:00
### Type aliases
To define a new type `NewType` as an alias for `ExistingType` ,
do `type NewType = ExistingType` .< br />
This is a special case of a [sum type ](#sum-types ) declaration.
2020-06-26 20:36:40 +03:00
### Option/Result types and error handling
2019-11-21 04:36:55 +03:00
2020-07-14 20:24:59 +03:00
Option types are declared with `?Type` :
2019-11-21 04:36:55 +03:00
```v
struct User {
2020-12-06 00:54:41 +03:00
id int
name string
2019-11-21 04:36:55 +03:00
}
struct Repo {
2020-12-06 00:54:41 +03:00
users []User
2019-11-21 04:36:55 +03:00
}
fn (r Repo) find_user_by_id(id int) ?User {
2020-12-06 00:54:41 +03:00
for user in r.users {
if user.id == id {
// V automatically wraps this into an option type
return user
}
}
return error('User $id not found')
2019-11-21 04:36:55 +03:00
}
fn main() {
2020-12-06 00:54:41 +03:00
repo := Repo{
2021-01-23 11:33:22 +03:00
users: [User{1, 'Andrew'}, User{2, 'Bob'}, User{10, 'Charles'}]
2020-12-06 00:54:41 +03:00
}
user := repo.find_user_by_id(10) or { // Option types must be handled by `or` blocks
return
}
println(user.id) // "10"
println(user.name) // "Charles"
2019-11-21 04:36:55 +03:00
}
```
V combines `Option` and `Result` into one type, so you don't need to decide which one to use.
2020-05-07 19:01:09 +03:00
The amount of work required to "upgrade" a function to an optional function is minimal;
2019-11-21 04:36:55 +03:00
you have to add a `?` to the return type and return an error when something goes wrong.
2020-10-20 21:14:56 +03:00
If you don't need to return an error message, you can simply `return none`
(this is a more efficient equivalent of `return error("")` ).
2019-11-21 04:36:55 +03:00
2020-05-07 19:01:09 +03:00
This is the primary mechanism for error handling in V. They are still values, like in Go,
2019-11-21 04:36:55 +03:00
but the advantage is that errors can't be unhandled, and handling them is a lot less verbose.
2020-07-11 11:26:22 +03:00
Unlike other languages, V does not handle exceptions with `throw/try/catch` blocks.
2019-11-21 04:36:55 +03:00
`err` is defined inside an `or` block and is set to the string message passed
to the `error()` function. `err` is empty if `none` was returned.
2020-11-03 03:04:14 +03:00
```v oksyntax
2019-11-21 04:36:55 +03:00
user := repo.find_user_by_id(7) or {
2020-12-06 00:54:41 +03:00
println(err) // "User 7 not found"
return
2019-11-21 04:36:55 +03:00
}
```
2020-07-14 20:24:59 +03:00
### Handling optionals
2020-07-12 20:27:41 +03:00
2020-07-15 22:57:41 +03:00
There are four ways of handling an optional. The first method is to
2020-07-14 20:24:59 +03:00
propagate the error:
2019-11-21 04:36:55 +03:00
```v
2020-07-14 20:24:59 +03:00
import net.http
fn f(url string) ?string {
2020-12-06 00:54:41 +03:00
resp := http.get(url) ?
return resp.text
2020-07-14 20:24:59 +03:00
}
2019-11-21 04:36:55 +03:00
```
2020-08-12 20:14:13 +03:00
`http.get` returns `?http.Response` . Because `?` follows the call, the
error will be propagated to the caller of `f` . When using `?` after a
function call producing an optional, the enclosing function must return
an optional as well. If error propagation is used in the `main()`
function it will `panic` instead, since the error cannot be propagated
2020-07-14 20:24:59 +03:00
any further.
2020-05-07 19:01:09 +03:00
2020-07-14 20:24:59 +03:00
The body of `f` is essentially a condensed version of:
2019-11-21 04:36:55 +03:00
2020-11-03 03:04:14 +03:00
```v ignore
2021-02-28 23:20:21 +03:00
resp := http.get(url) or { return err }
2020-07-14 20:24:59 +03:00
return resp.text
2019-11-21 04:36:55 +03:00
```
2020-07-14 20:24:59 +03:00
---
The second method is to break from execution early:
2020-07-12 20:27:41 +03:00
2020-11-03 03:04:14 +03:00
```v oksyntax
2020-12-06 00:54:41 +03:00
user := repo.find_user_by_id(7) or { return }
2020-07-12 20:27:41 +03:00
```
2020-10-20 21:14:56 +03:00
Here, you can either call `panic()` or `exit()` , which will stop the execution of the
entire program, or use a control flow statement (`return`, `break` , `continue` , etc)
to break from the current block.
2020-07-12 20:27:41 +03:00
Note that `break` and `continue` can only be used inside a `for` loop.
2020-10-20 21:14:56 +03:00
V does not have a way to forcibly "unwrap" an optional (as other languages do,
2021-02-28 23:20:21 +03:00
for instance Rust's `unwrap()` or Swift's `!` ). To do this, use `or { panic(err.msg) }` instead.
2020-07-15 22:57:41 +03:00
2020-07-14 20:24:59 +03:00
---
2020-10-20 21:14:56 +03:00
The third method is to provide a default value at the end of the `or` block.
In case of an error, that value would be assigned instead,
so it must have the same type as the content of the `Option` being handled.
2020-07-12 20:27:41 +03:00
2021-01-10 22:21:37 +03:00
```v
2020-07-12 20:27:41 +03:00
fn do_something(s string) ?string {
2020-12-06 00:54:41 +03:00
if s == 'foo' {
return 'foo'
}
return error('invalid string') // Could be `return none` as well
2020-07-12 20:27:41 +03:00
}
a := do_something('foo') or { 'default' } // a will be 'foo'
b := do_something('bar') or { 'default' } // b will be 'default'
2020-11-03 03:04:14 +03:00
println(a)
println(b)
2020-07-12 20:27:41 +03:00
```
2020-07-15 22:57:41 +03:00
---
The fourth method is to use `if` unwrapping:
```v
2020-11-03 03:04:14 +03:00
import net.http
2020-12-06 00:54:41 +03:00
2020-11-03 03:04:14 +03:00
if resp := http.get('https://google.com') {
2020-12-06 00:54:41 +03:00
println(resp.text) // resp is a http.Response, not an optional
2020-07-15 22:57:41 +03:00
} else {
2020-12-06 00:54:41 +03:00
println(err)
2020-07-15 22:57:41 +03:00
}
```
2020-08-12 20:14:13 +03:00
Above, `http.get` returns a `?http.Response` . `resp` is only in scope for the first
2020-07-15 22:57:41 +03:00
`if` branch. `err` is only in scope for the `else` branch.
2020-04-23 06:35:33 +03:00
2019-11-21 04:36:55 +03:00
## Generics
2020-11-03 03:04:14 +03:00
```v wip
2019-11-21 04:36:55 +03:00
struct Repo< T > {
db DB
}
2021-03-04 16:07:13 +03:00
struct User {
id int
name string
}
struct Post {
id int
user_id int
title string
body string
}
2019-11-21 04:36:55 +03:00
fn new_repo< T > (db DB) Repo< T > {
return Repo< T > {db: db}
}
// This is a generic function. V will generate it for every type it's used with.
fn (r Repo< T > ) find_by_id(id int) ?T {
table_name := T.name // in this example getting the name of the type gives us the table name
return r.db.query_one< T > ('select * from $table_name where id = ?', id)
}
db := new_db()
2020-10-24 20:37:14 +03:00
users_repo := new_repo< User > (db) // returns Repo< User >
posts_repo := new_repo< Post > (db) // returns Repo< Post >
user := users_repo.find_by_id(1)? // find_by_id< User >
post := posts_repo.find_by_id(1)? // find_by_id< Post >
2019-11-21 04:36:55 +03:00
```
2020-10-24 20:37:14 +03:00
Currently generic function definitions must declare their type parameters, but in
future V will infer generic type parameters from single-letter type names in
2020-12-01 18:09:33 +03:00
runtime parameter types. This is why `find_by_id` can omit `<T>` , because the
2020-10-24 20:37:14 +03:00
receiver argument `r` uses a generic type `T` .
2019-11-21 04:36:55 +03:00
2020-08-01 16:34:23 +03:00
Another example:
2021-01-10 22:21:37 +03:00
```v
2020-10-24 20:37:14 +03:00
fn compare< T > (a T, b T) int {
2020-12-06 00:54:41 +03:00
if a < b {
return -1
}
if a > b {
return 1
}
return 0
2020-08-01 16:34:23 +03:00
}
2020-10-24 20:37:14 +03:00
// compare< int >
println(compare(1, 0)) // Outputs: 1
println(compare(1, 1)) // 0
println(compare(1, 2)) // -1
// compare< string >
println(compare('1', '0')) // Outputs: 1
println(compare('1', '1')) // 0
println(compare('1', '2')) // -1
// compare< f64 >
println(compare(1.1, 1.0)) // Outputs: 1
println(compare(1.1, 1.1)) // 0
println(compare(1.1, 1.2)) // -1
2020-08-01 16:34:23 +03:00
```
2019-11-21 04:36:55 +03:00
## Concurrency
2021-01-16 00:40:26 +03:00
### Spawning Concurrent Tasks
V's model of concurrency is very similar to Go's. To run `foo()` concurrently in
a different thread, just call it with `go foo()` :
2019-11-21 04:36:55 +03:00
2021-01-16 00:40:26 +03:00
```v
import math
fn p(a f64, b f64) { // ordinary function without return value
c := math.sqrt(a * a + b * b)
println(c)
}
fn main() {
go p(3, 4)
// p will be run in parallel thread
}
```
Sometimes it is necessary to wait until a parallel thread has finished. This can
be done by assigning a *handle* to the started thread and calling the `wait()` method
to this handle later:
```v
import math
fn p(a f64, b f64) { // ordinary function without return value
c := math.sqrt(a * a + b * b)
println(c) // prints `5`
}
fn main() {
h := go p(3, 4)
// p() runs in parallel thread
h.wait()
// p() has definitely finished
}
```
This approach can also be used to get a return value from a function that is run in a
parallel thread. There is no need to modify the function itself to be able to call it
concurrently.
```v
import math { sqrt }
fn get_hypot(a f64, b f64) f64 { // ordinary function returning a value
c := sqrt(a * a + b * b)
return c
}
fn main() {
g := go get_hypot(54.06, 2.08) // spawn thread and get handle to it
h1 := get_hypot(2.32, 16.74) // do some other calculation here
h2 := g.wait() // get result from spawned thread
println('Results: $h1, $h2') // prints `Results: 16.9, 54.1`
}
```
2021-02-24 12:41:12 +03:00
If there is a large number of tasks, it might be easier to manage them
using an array of threads.
2019-11-21 04:36:55 +03:00
2020-08-02 00:08:57 +03:00
```v
import time
2021-02-24 12:41:12 +03:00
fn task(id int, duration int) {
2020-12-06 00:54:41 +03:00
println('task $id begin')
2021-02-28 19:01:31 +03:00
time.sleep(duration * time.millisecond)
2020-12-06 00:54:41 +03:00
println('task $id end')
2020-08-02 00:08:57 +03:00
}
fn main() {
2021-02-24 12:41:12 +03:00
mut threads := []thread{}
threads < < go task ( 1 , 500 )
threads < < go task ( 2 , 900 )
threads < < go task ( 3 , 100 )
threads.wait()
2020-12-06 00:54:41 +03:00
println('done')
2020-08-02 00:08:57 +03:00
}
2020-12-06 00:54:41 +03:00
// Output:
// task 1 begin
// task 2 begin
// task 3 begin
// task 3 end
// task 1 end
// task 2 end
// done
2020-08-02 00:08:57 +03:00
```
2021-02-24 12:41:12 +03:00
Additionally for threads that return the same type, calling `wait()`
on the thread array will return all computed values.
```v
fn expensive_computing(i int) int {
return i * i
}
fn main() {
mut threads := []thread int{}
for i in 1 .. 10 {
threads < < go expensive_computing ( i )
}
// Join all tasks
r := threads.wait()
println('All jobs finished: $r')
}
// Output: All jobs finished: [1, 4, 9, 16, 25, 36, 49, 64, 81]
```
2020-09-20 17:20:56 +03:00
### Channels
Channels are the preferred way to communicate between coroutines. V's channels work basically like
those in Go. You can push objects into a channel on one end and pop objects from the other end.
Channels can be buffered or unbuffered and it is possible to `select` from multiple channels.
#### Syntax and Usage
2020-10-25 15:55:57 +03:00
Channels have the type `chan objtype` . An optional buffer length can specified as the `cap` property
2020-09-20 17:20:56 +03:00
in the declaration:
2021-01-10 22:21:37 +03:00
```v
ch := chan int{} // unbuffered - "synchronous"
2020-09-20 17:20:56 +03:00
ch2 := chan f64{cap: 100} // buffer length 100
```
Channels do not have to be declared as `mut` . The buffer length is not part of the type but
a property of the individual channel object. Channels can be passed to coroutines like normal
variables:
```v
fn f(ch chan int) {
2020-12-06 00:54:41 +03:00
// ...
2020-09-20 17:20:56 +03:00
}
fn main() {
2020-12-06 00:54:41 +03:00
ch := chan int{}
go f(ch)
// ...
2020-09-20 17:20:56 +03:00
}
```
Objects can be pushed to channels using the arrow operator. The same operator can be used to
pop objects from the other end:
2021-01-10 22:21:37 +03:00
```v
2021-04-25 18:20:12 +03:00
// make buffered channels so pushing does not block (if there is room in the buffer)
ch := chan int{cap: 1}
ch2 := chan f64{cap: 1}
2020-09-20 17:20:56 +03:00
n := 5
2021-01-10 22:21:37 +03:00
// push
2021-04-25 18:20:12 +03:00
ch < - n
ch2 < - 7 . 3
2020-09-20 17:20:56 +03:00
mut y := f64(0.0)
2021-01-10 22:21:37 +03:00
m := < -ch / / pop creating new variable
y = < -ch2 / / pop into existing variable
2020-09-20 17:20:56 +03:00
```
A channel can be closed to indicate that no further objects can be pushed. Any attempt
to do so will then result in a runtime panic (with the exception of `select` and
`try_push()` - see below). Attempts to pop will return immediately if the
associated channel has been closed and the buffer is empty. This situation can be
handled using an or branch (see [Handling Optionals ](#handling-optionals )).
2020-11-03 03:04:14 +03:00
```v wip
2021-01-13 23:54:11 +03:00
ch := chan int{}
ch2 := chan f64{}
2020-11-03 03:04:14 +03:00
// ...
2020-09-20 17:20:56 +03:00
ch.close()
2020-11-03 03:04:14 +03:00
// ...
2020-09-20 17:20:56 +03:00
m := < -ch or {
println('channel has been closed')
}
// propagate error
y := < -ch2 ?
```
#### Channel Select
2020-10-20 21:14:56 +03:00
The `select` command allows monitoring several channels at the same time
without noticeable CPU load. It consists of a list of possible transfers and associated branches
of statements - similar to the [match ](#match ) command:
2021-05-27 12:47:13 +03:00
```v
2020-11-03 03:04:14 +03:00
import time
2021-05-27 12:47:13 +03:00
fn main() {
ch := chan f64{}
ch2 := chan f64{}
ch3 := chan f64{}
mut b := 0.0
c := 1.0
// ... setup go threads that will send on ch/ch2
go fn (the_channel chan f64) {
time.sleep(5 * time.millisecond)
the_channel < - 1 . 0
}(ch)
go fn (the_channel chan f64) {
time.sleep(1 * time.millisecond)
the_channel < - 1 . 0
}(ch2)
go fn (the_channel chan f64) {
_ := < -the_channel
}(ch3)
2021-10-18 21:03:27 +03:00
2021-05-27 12:47:13 +03:00
select {
a := < -ch {
// do something with `a`
eprintln('> a: $a')
}
b = < -ch2 {
// do something with predeclared variable `b`
eprintln('> b: $b')
}
ch3 < - c {
// do something if `c` was sent
time.sleep(5 * time.millisecond)
eprintln('> c: $c was send on channel ch3')
}
2021-07-23 23:24:27 +03:00
500 * time.millisecond {
2021-05-27 12:47:13 +03:00
// do something if no channel has become ready within 0.5s
eprintln('> more than 0.5s passed without a channel being ready')
}
}
eprintln('> done')
2020-12-01 18:09:33 +03:00
}
2020-09-20 17:20:56 +03:00
```
The timeout branch is optional. If it is absent `select` waits for an unlimited amount of time.
It is also possible to proceed immediately if no channel is ready in the moment `select` is called
2021-10-31 12:32:31 +03:00
by adding an `else { ... }` branch. `else` and `<timeout>` are mutually exclusive.
2020-09-20 17:20:56 +03:00
2020-10-20 21:14:56 +03:00
The `select` command can be used as an *expression* of type `bool`
that becomes `false` if all channels are closed:
2020-11-03 03:04:14 +03:00
```v wip
2020-09-20 17:20:56 +03:00
if select {
ch < - a {
2020-11-03 03:04:14 +03:00
// ...
2020-09-20 17:20:56 +03:00
}
2020-09-21 08:34:04 +03:00
} {
// channel was open
2020-09-20 17:20:56 +03:00
} else {
// channel is closed
}
```
#### Special Channel Features
For special purposes there are some builtin properties and methods:
2021-01-10 22:21:37 +03:00
```v
struct Abc {
x int
}
2020-11-03 03:04:14 +03:00
a := 2.13
2021-01-13 23:54:11 +03:00
ch := chan f64{}
2021-01-10 22:21:37 +03:00
res := ch.try_push(a) // try to perform `ch <- a`
2020-11-03 03:04:14 +03:00
println(res)
2021-01-10 22:21:37 +03:00
l := ch.len // number of elements in queue
c := ch.cap // maximum queue length
2021-01-22 10:37:29 +03:00
is_closed := ch.closed // bool flag - has `ch` been closed
2020-11-03 03:04:14 +03:00
println(l)
println(c)
2021-01-13 23:54:11 +03:00
mut b := Abc{}
ch2 := chan Abc{}
2021-04-19 11:41:21 +03:00
res2 := ch2.try_pop(mut b) // try to perform `b = <-ch2`
2020-09-20 17:20:56 +03:00
```
2020-12-06 00:54:41 +03:00
2020-10-20 21:14:56 +03:00
The `try_push/pop()` methods will return immediately with one of the results
`.success` , `.not_ready` or `.closed` - dependent on whether the object has been transferred or
the reason why not.
Usage of these methods and properties in production is not recommended -
2021-01-22 10:37:29 +03:00
algorithms based on them are often subject to race conditions. Especially `.len` and
`.closed` should not be used to make decisions.
Use `or` branches, error propagation or `select` instead (see [Syntax and Usage ](#syntax-and-usage )
and [Channel Select ](#channel-select ) above).
2020-09-20 17:20:56 +03:00
2021-01-16 00:40:26 +03:00
### Shared Objects
2020-10-20 21:14:56 +03:00
Data can be exchanged between a coroutine and the calling thread via a shared variable.
2021-01-13 23:54:11 +03:00
Such variables should be created as `shared` and passed to the coroutine as such, too.
The underlying `struct` contains a hidden *mutex* that allows locking concurrent access
using `rlock` for read-only and `lock` for read/write access.
2020-06-27 00:55:34 +03:00
```v
struct St {
mut:
2021-10-18 21:03:27 +03:00
x int // data to be shared
2020-06-27 00:55:34 +03:00
}
2021-01-13 23:54:11 +03:00
fn (shared b St) g() {
lock b {
// read/modify/write b.x
}
2020-06-27 00:55:34 +03:00
}
2021-01-13 23:54:11 +03:00
fn main() {
2021-02-01 23:43:45 +03:00
shared a := St{
2020-06-27 00:55:34 +03:00
x: 10
}
go a.g()
2021-01-13 23:54:11 +03:00
// ...
rlock a {
// read a.x
}
2020-06-27 00:55:34 +03:00
}
```
2021-02-01 23:43:45 +03:00
Shared variables must be structs, arrays or maps.
2020-06-27 00:55:34 +03:00
2019-11-21 04:36:55 +03:00
## Decoding JSON
```v
import json
2020-09-26 12:34:26 +03:00
struct Foo {
x int
}
2019-11-21 04:36:55 +03:00
2020-12-06 00:54:41 +03:00
struct User {
2021-07-25 17:27:37 +03:00
// Adding a [required] attribute will make decoding fail, if that
// field is not present in the input.
// If a field is not [required], but is missing, it will be assumed
// to have its default value, like 0 for numbers, or '' for strings,
// and decoding will not fail.
name string [required]
2021-01-12 06:38:43 +03:00
age int
2020-12-06 00:54:41 +03:00
// Use the `skip` attribute to skip certain fields
2021-01-12 06:38:43 +03:00
foo Foo [skip]
2020-12-06 00:54:41 +03:00
// If the field name is different in JSON, it can be specified
last_name string [json: lastName]
2019-11-21 04:36:55 +03:00
}
data := '{ "name": "Frodo", "lastName": "Baggins", "age": 25 }'
user := json.decode(User, data) or {
2021-07-25 17:27:37 +03:00
eprintln('Failed to decode json, error: $err')
2020-12-06 00:54:41 +03:00
return
2019-11-21 04:36:55 +03:00
}
println(user.name)
println(user.last_name)
println(user.age)
2020-09-26 12:34:26 +03:00
// You can also decode JSON arrays:
sfoos := '[{"x":123},{"x":456}]'
2020-12-06 00:54:41 +03:00
foos := json.decode([]Foo, sfoos) ?
2020-09-26 12:34:26 +03:00
println(foos[0].x)
println(foos[1].x)
2019-11-21 04:36:55 +03:00
```
2020-05-07 19:01:09 +03:00
Because of the ubiquitous nature of JSON, support for it is built directly into V.
2019-11-21 04:36:55 +03:00
2020-10-20 21:14:56 +03:00
The `json.decode` function takes two arguments:
the first is the type into which the JSON value should be decoded and
the second is a string containing the JSON data.
2019-11-21 04:36:55 +03:00
2020-10-20 21:14:56 +03:00
V generates code for JSON encoding and decoding.
No runtime reflection is used. This results in much better performance.
2019-11-21 04:36:55 +03:00
## Testing
2020-09-25 19:25:14 +03:00
### Asserts
2020-10-15 13:32:28 +03:00
```v
2020-12-06 00:54:41 +03:00
fn foo(mut v []int) {
v[0] = 1
}
2020-11-03 03:04:14 +03:00
mut v := [20]
2020-09-25 19:25:14 +03:00
foo(mut v)
2020-11-03 03:04:14 +03:00
assert v[0] < 4
2020-10-15 13:32:28 +03:00
```
An `assert` statement checks that its expression evaluates to `true` . If an assert fails,
2020-09-25 19:25:14 +03:00
the program will abort. Asserts should only be used to detect programming errors. When an
2020-10-15 13:32:28 +03:00
assert fails it is reported to *stderr* , and the values on each side of a comparison operator
(such as `<` , `==` ) will be printed when possible. This is useful to easily find an
2020-09-25 19:25:14 +03:00
unexpected value. Assert statements can be used in any function.
### Test files
2019-11-21 04:36:55 +03:00
```v
// hello.v
2020-09-29 23:48:56 +03:00
module main
2020-12-06 00:54:41 +03:00
2020-09-29 23:48:56 +03:00
fn hello() string {
2020-12-06 00:54:41 +03:00
return 'Hello world'
2019-11-21 04:36:55 +03:00
}
2020-12-06 00:54:41 +03:00
2020-09-29 23:48:56 +03:00
fn main() {
2020-12-06 00:54:41 +03:00
println(hello())
2020-09-29 23:48:56 +03:00
}
2020-09-25 12:51:39 +03:00
```
2020-12-06 00:54:41 +03:00
2020-11-03 03:04:14 +03:00
```v failcompile
2021-10-05 22:49:51 +03:00
// hello_test.v
2020-09-29 23:48:56 +03:00
module main
2021-04-12 15:13:00 +03:00
2019-11-21 04:36:55 +03:00
fn test_hello() {
2021-04-12 15:13:00 +03:00
assert hello() == 'Hello world'
2019-11-21 04:36:55 +03:00
}
```
2020-10-15 13:32:28 +03:00
To run the test above, use `v hello_test.v` . This will check that the function `hello` is
producing the correct output. V executes all test functions in the file.
2019-11-21 04:36:55 +03:00
2020-09-25 19:25:14 +03:00
* All test functions have to be inside a test file whose name ends in `_test.v` .
* Test function names must begin with `test_` to mark them for execution.
* Normal functions can also be defined in test files, and should be called manually. Other
symbols can also be defined in test files e.g. types.
2020-10-25 15:55:57 +03:00
* There are two kinds of tests: external and internal.
* Internal tests must *declare* their module, just like all other .v
files from the same module. Internal tests can even call private functions in
2020-10-15 13:32:28 +03:00
the same module.
2020-10-25 15:55:57 +03:00
* External tests must *import* the modules which they test. They do not
2020-10-15 13:32:28 +03:00
have access to the private functions/types of the modules. They can test only
2020-09-29 23:48:56 +03:00
the external/public API that a module provides.
In the example above, `test_hello` is an internal test, that can call
the private function `hello()` because `hello_test.v` has `module main` ,
2020-10-15 13:32:28 +03:00
just like `hello.v` , i.e. both are part of the same module. Note also that
since `module main` is a regular module like the others, internal tests can
2020-09-29 23:48:56 +03:00
be used to test private functions in your main program .v files too.
2020-09-25 12:51:39 +03:00
2021-10-05 22:49:51 +03:00
You can also define these special test functions in a test file:
2020-09-25 19:25:14 +03:00
* `testsuite_begin` which will be run *before* all other test functions.
* `testsuite_end` which will be run *after* all other test functions.
2019-11-21 04:36:55 +03:00
2021-05-21 14:18:13 +03:00
If a test function has an error return type, any propagated errors will fail the test:
2021-07-03 15:24:01 +03:00
```v
2021-05-21 14:18:13 +03:00
import strconv
fn test_atoi() ? {
assert strconv.atoi('1') ? == 1
assert strconv.atoi('one') ? == 1 // test will fail
}
```
2020-09-25 19:25:14 +03:00
#### Running tests
2020-02-08 19:00:35 +03:00
2020-09-25 19:25:14 +03:00
To run test functions in an individual test file, use `v foo_test.v` .
2020-02-08 19:00:35 +03:00
2020-10-15 13:32:28 +03:00
To test an entire module, use `v test mymodule` . You can also use `v test .` to test
everything inside your current folder (and subfolders). You can pass the `-stats`
2020-09-25 19:25:14 +03:00
option to see more details about the individual tests run.
2019-11-21 04:36:55 +03:00
2021-06-02 21:19:03 +03:00
You can put additional test data, including .v source files in a folder, named
`testdata` , right next to your _test.v files. V's test framework will *ignore*
2021-10-18 21:03:27 +03:00
such folders, while scanning for tests to run. This is useful, if you want to
2021-06-25 12:11:29 +03:00
put .v files with invalid V source code, or other tests, including known
failing ones, that should be run in a specific way/options by a parent _test.v
2021-06-02 21:19:03 +03:00
file.
NB: the path to the V compiler, is available through @VEXE , so a _test.v
file, can easily run *other* test files like this:
```v oksyntax
import os
fn test_subtest() {
res := os.execute('${@VEXE} other_test.v')
assert res.exit_code == 1
assert res.output.contains('other_test.v does not exist')
}
```
2019-11-21 04:36:55 +03:00
## Memory management
2020-12-19 08:34:07 +03:00
V avoids doing unnecessary allocations in the first place by using value types,
string buffers, promoting a simple abstraction-free code style.
2020-05-07 13:30:41 +03:00
2020-12-19 08:34:07 +03:00
Most objects (~90-100%) are freed by V's autofree engine: the compiler inserts
necessary free calls automatically during compilation. Remaining small percentage
of objects is freed via reference counting.
The developer doesn't need to change anything in their code. "It just works", like in
Python, Go, or Java, except there's no heavy GC tracing everything or expensive RC for
each object.
2021-03-13 10:06:59 +03:00
### Control
2021-04-29 02:17:37 +03:00
You can take advantage of V's autofree engine and define a `free()` method on custom
2021-03-13 10:06:59 +03:00
data types:
```v
struct MyType {}
[unsafe]
fn (data & MyType) free() {
// ...
}
```
2021-04-29 02:17:37 +03:00
Just as the compiler frees C data types with C's `free()` , it will statically insert
2021-03-13 10:06:59 +03:00
`free()` calls for your data type at the end of each variable's lifetime.
2020-12-19 08:34:07 +03:00
For developers willing to have more low level control, autofree can be disabled with
2021-01-10 22:21:37 +03:00
`-manualfree` , or by adding a `[manualfree]` on each function that wants manage its
2021-03-13 10:06:59 +03:00
memory manually. (See [attributes ](#attributes )).
2020-12-19 08:34:07 +03:00
2021-03-13 10:06:59 +03:00
_Note: right now autofree is hidden behind the -autofree flag. It will be enabled by
default in V 0.3. If autofree is not used, V programs will leak memory._
2020-12-19 08:34:07 +03:00
2021-03-13 10:06:59 +03:00
### Examples
2019-11-21 04:36:55 +03:00
```v
2020-11-03 03:04:14 +03:00
import strings
2020-12-06 00:54:41 +03:00
2020-11-03 03:04:14 +03:00
fn draw_text(s string, x int, y int) {
2020-12-06 00:54:41 +03:00
// ...
2019-11-21 04:36:55 +03:00
}
fn draw_scene() {
2020-12-06 00:54:41 +03:00
// ...
name1 := 'abc'
name2 := 'def ghi'
draw_text('hello $name1', 10, 10)
draw_text('hello $name2', 100, 10)
draw_text(strings.repeat(`X`, 10000), 10, 50)
// ...
2019-11-21 04:36:55 +03:00
}
```
The strings don't escape `draw_text` , so they are cleaned up when
the function exits.
2021-03-13 10:06:59 +03:00
In fact, with the `-prealloc` flag, the first two calls won't result in any allocations at all.
These two strings are small, so V will use a preallocated buffer for them.
2019-11-21 04:36:55 +03:00
```v
2020-12-06 00:54:41 +03:00
struct User {
name string
}
2019-11-21 04:36:55 +03:00
fn test() []int {
2020-12-06 00:54:41 +03:00
number := 7 // stack variable
user := User{} // struct allocated on stack
numbers := [1, 2, 3] // array allocated on heap, will be freed as the function exits
println(number)
println(user)
println(numbers)
numbers2 := [4, 5, 6] // array that's being returned, won't be freed here
return numbers2
2019-11-21 04:36:55 +03:00
}
```
2021-07-16 21:40:51 +03:00
### Stack and Heap
#### Stack and Heap Basics
Like with most other programming languages there are two locations where data can
be stored:
* The *stack* allows fast allocations with almost zero administrative overhead. The
stack grows and shrinks with the function call depth – so every called
function has its stack segment that remains valid until the function returns.
No freeing is necessary, however, this also means that a reference to a stack
object becomes invalid on function return. Furthermore stack space is
limited (typically to a few Megabytes per thread).
* The *heap* is a large memory area (typically some Gigabytes) that is administrated
by the operating system. Heap objects are allocated and freed by special function
calls that delegate the administrative tasks to the OS. This means that they can
remain valid across several function calls, however, the administration is
expensive.
#### V's default approach
Due to performance considerations V tries to put objects on the stack if possible
but allocates them on the heap when obviously necessary. Example:
```v
struct MyStruct {
n int
}
2021-07-17 17:47:42 +03:00
struct RefStruct {
r & MyStruct
}
2021-07-16 21:40:51 +03:00
fn main() {
q, w := f()
println('q: $q.r.n, w: $w.n')
}
fn f() (RefStruct, & MyStruct) {
a := MyStruct{
n: 1
}
b := MyStruct{
n: 2
}
c := MyStruct{
n: 3
}
e := RefStruct{
r: & b
}
x := a.n + c.n
println('x: $x')
return e, & c
}
```
Here `a` is stored on the stack since it's address never leaves the function `f()` .
However a reference to `b` is part of `e` which is returned. Also a reference to
2021-07-28 08:21:42 +03:00
`c` is returned. For this reason `b` and `c` will be heap allocated.
2021-07-16 21:40:51 +03:00
Things become less obvious when a reference to an object is passed as function argument:
```v
struct MyStruct {
mut:
n int
}
fn main() {
mut q := MyStruct{
n: 7
}
w := MyStruct{
n: 13
}
x := q.f(& w) // references of `q` and `w` are passed
println('q: $q\nx: $x')
}
fn (mut a MyStruct) f(b & MyStruct) int {
a.n += b.n
x := a.n * b.n
return x
}
```
Here the call `q.f(&w)` passes references to `q` and `w` because `a` is
`mut` and `b` is of type `&MyStruct` in `f()` 's declaration, so technically
these references are leaving `main()` . However the *lifetime* of these
2021-07-17 17:47:42 +03:00
references lies inside the scope of `main()` so `q` and `w` are allocated
2021-07-16 21:40:51 +03:00
on the stack.
2021-07-28 08:21:42 +03:00
#### Manual Control for Stack and Heap
2021-07-16 21:40:51 +03:00
In the last example the V compiler could put `q` and `w` on the stack
because it assumed that in the call `q.f(&w)` these references were only
used for reading and modifying the referred values – and not to pass the
2021-07-17 17:47:42 +03:00
references themselves somewhere else. This can be seen in a way that the
2021-07-16 21:40:51 +03:00
references to `q` and `w` are only *borrowed* to `f()` .
2021-07-17 17:47:42 +03:00
Things become different if `f()` is doing something with a reference itself:
2021-07-16 21:40:51 +03:00
```v
struct RefStruct {
mut:
r & MyStruct
}
// see discussion below
[heap]
struct MyStruct {
n int
}
fn main() {
m := MyStruct{}
mut r := RefStruct{
r: & m
}
r.g()
println('r: $r')
}
fn (mut r RefStruct) g() {
s := MyStruct{
n: 7
}
r.f(& s) // reference to `s` inside `r` is passed back to `main() `
}
fn (mut r RefStruct) f(s & MyStruct) {
r.r = s // would trigger error without `[heap]`
}
```
Here `f()` looks quite innocent but is doing nasty things – it inserts a
reference to `s` into `r` . The problem with this is that `s` lives only as long
as `g()` is running but `r` is used in `main()` after that. For this reason
the compiler would complain about the assignment in `f()` because `s` *"might
refer to an object stored on stack"*. The assumption made in `g()` that the call
2021-07-28 08:21:42 +03:00
`r.f(&s)` would only borrow the reference to `s` is wrong.
2021-07-16 21:40:51 +03:00
A solution to this dilemma is the `[heap]` attribute at the declaration of
`struct MyStruct` . It instructs the compiler to *always* allocate `MyStruct` -objects
2021-07-17 17:47:42 +03:00
on the heap. This way the reference to `s` remains valid even after `g()` returns.
2021-07-16 21:40:51 +03:00
The compiler takes into consideration that `MyStruct` objects are always heap
allocated when checking `f()` and allows assigning the reference to `s` to the
`r.r` field.
There is a pattern often seen in other programming languages:
```v failcompile
fn (mut a MyStruct) f() & MyStruct {
// do something with a
return & a // would return address of borrowed object
}
```
2021-07-17 17:47:42 +03:00
Here `f()` is passed a reference `a` as receiver that is passed back to the caller and returned
as result at the same time. The intention behind such a declaration is method chaining like
2021-07-16 21:40:51 +03:00
`y = x.f().g()` . However, the problem with this approach is that a second reference
to `a` is created – so it is not only borrowed and `MyStruct` has to be
declared as `[heap]` .
In V the better approach is:
```v
struct MyStruct {
mut:
n int
}
fn (mut a MyStruct) f() {
// do something with `a`
}
fn (mut a MyStruct) g() {
// do something else with `a`
}
fn main() {
x := MyStruct{} // stack allocated
mut y := x
y.f()
y.g()
// instead of `mut y := x.f().g()
}
```
This way the `[heap]` attribute can be avoided – resulting in better performance.
However, stack space is very limited as mentioned above. For this reason the `[heap]`
attribute might be suitable for very large structures even if not required by use cases
like those mentioned above.
There is an alternative way to manually control allocation on a case to case basis. This
approach is not recommended but shown here for the sake of completeness:
```v
struct MyStruct {
n int
}
2021-07-17 17:47:42 +03:00
struct RefStruct {
mut:
r & MyStruct
2021-07-16 21:40:51 +03:00
}
2021-07-17 17:47:42 +03:00
// simple function - just to overwrite stack segment previously used by `g()`
2021-07-16 21:40:51 +03:00
fn use_stack() {
x := 7.5
y := 3.25
z := x + y
println('$x $y $z')
}
fn main() {
m := MyStruct{}
mut r := RefStruct{
r: & m
}
r.g()
use_stack() // to erase invalid stack contents
println('r: $r')
}
2021-07-17 17:47:42 +03:00
fn (mut r RefStruct) g() {
s := & MyStruct{ // `s` explicitly refers to a heap object
n: 7
}
// change `&MyStruct` -> `MyStruct` above and `r.f(s)` -> `r.f(&s)` below
// to see data in stack segment being overwritten
r.f(s)
}
fn (mut r RefStruct) f(s & MyStruct) {
r.r = unsafe { s } // override compiler check
}
2021-07-16 21:40:51 +03:00
```
Here the compiler check is suppressed by the `unsafe` block. To make `s` be heap
allocated even without `[heap]` attribute the `struct` literal is prefixed with
an ampersand: `&MyStruct{...}` .
This last step would not be required by the compiler but without it the reference
inside `r` becomes invalid (the memory area pointed to will be overwritten by
`use_stack()` ) and the program might crash (or at least produce an unpredictable
final output). That's why this approach is *unsafe* and should be avoided!
2019-11-21 04:36:55 +03:00
## ORM
2020-07-23 01:35:42 +03:00
(This is still in an alpha state)
2019-11-21 04:36:55 +03:00
2021-04-25 18:57:55 +03:00
V has a built-in ORM (object-relational mapping) which supports SQLite, MySQL and Postgres,
but soon it will support MS SQL and Oracle.
2019-11-21 04:36:55 +03:00
2020-05-07 19:01:09 +03:00
V's ORM provides a number of benefits:
2019-11-21 04:36:55 +03:00
2020-07-23 01:35:42 +03:00
- One syntax for all SQL dialects. (Migrating between databases becomes much easier.)
- Queries are constructed using V's syntax. (There's no need to learn another syntax.)
- Safety. (All queries are automatically sanitised to prevent SQL injection.)
- Compile time checks. (This prevents typos which can only be caught during runtime.)
2020-10-20 21:14:56 +03:00
- Readability and simplicity. (You don't need to manually parse the results of a query and
then manually construct objects from the parsed results.)
2019-11-21 04:36:55 +03:00
2021-01-10 22:21:37 +03:00
```v
2020-11-03 03:04:14 +03:00
import sqlite
2019-11-21 04:36:55 +03:00
2021-11-08 13:22:42 +03:00
// sets a custom table name. Default is struct name (case-sensitive)
[table: 'customers']
2021-01-10 22:21:37 +03:00
struct Customer {
2021-04-10 17:38:27 +03:00
id int [primary; sql: serial] // a field named `id` of integer type must be the first field
name string [nonull]
2021-01-10 22:21:37 +03:00
nr_orders int
2021-04-10 17:38:27 +03:00
country string [nonull]
2021-01-10 22:21:37 +03:00
}
2019-11-21 04:36:55 +03:00
2021-01-10 22:21:37 +03:00
db := sqlite.connect('customers.db') ?
2021-04-10 17:38:27 +03:00
2021-08-16 10:11:44 +03:00
// you can create tables:
// CREATE TABLE IF NOT EXISTS `Customer` (
// `id` INTEGER PRIMARY KEY,
// `name` TEXT NOT NULL,
// `nr_orders` INTEGER,
// `country` TEXT NOT NULL
// )
2021-04-10 17:38:27 +03:00
sql db {
create table Customer
}
2021-11-08 13:22:42 +03:00
// select count(*) from customers
2021-01-10 22:21:37 +03:00
nr_customers := sql db {
select count from Customer
}
2019-11-21 04:36:55 +03:00
println('number of all customers: $nr_customers')
// V syntax can be used to build queries
2021-01-10 22:21:37 +03:00
uk_customers := sql db {
select from Customer where country == 'uk' & & nr_orders > 0
}
2019-11-21 04:36:55 +03:00
println(uk_customers.len)
for customer in uk_customers {
2021-01-10 22:21:37 +03:00
println('$customer.id - $customer.name')
2019-11-21 04:36:55 +03:00
}
// by adding `limit 1` we tell V that there will be only one object
2021-01-10 22:21:37 +03:00
customer := sql db {
select from Customer where id == 1 limit 1
}
2019-11-21 04:36:55 +03:00
println('$customer.id - $customer.name')
// insert a new customer
2021-01-10 22:21:37 +03:00
new_customer := Customer{
name: 'Bob'
nr_orders: 10
}
sql db {
insert new_customer into Customer
}
2019-11-21 04:36:55 +03:00
```
2021-04-10 17:38:27 +03:00
For more examples and the docs, see < a href = 'https://github.com/vlang/v/tree/master/vlib/orm' > vlib/orm< / a > .
2020-06-21 17:12:16 +03:00
2020-05-01 11:04:00 +03:00
## Writing Documentation
2019-11-21 04:36:55 +03:00
The way it works is very similar to Go. It's very simple: there's no need to
2020-10-20 21:14:56 +03:00
write documentation separately for your code,
vdoc will generate it from docstrings in the source code.
2019-11-21 04:36:55 +03:00
Documentation for each function/type/const must be placed right before the declaration:
```v
// clearall clears all bits in the array
fn clearall() {
}
```
The comment must start with the name of the definition.
2020-11-30 20:49:57 +03:00
Sometimes one line isn't enough to explain what a function does, in that case comments should
span to the documented function using single line comments:
```v
// copy_all recursively copies all elements of the array by their value,
// if `dupes` is false all duplicate values are eliminated in the process.
fn copy_all(dupes bool) {
// ...
}
```
By convention it is preferred that comments are written in *present tense* .
2019-11-21 04:36:55 +03:00
An overview of the module must be placed in the first comment right after the module's name.
2020-05-01 11:55:48 +03:00
To generate documentation use vdoc, for example `v doc net.http` .
2020-05-01 11:04:00 +03:00
2021-08-19 10:20:43 +03:00
### Newlines in Documentation Comments
Comments spanning multiple lines are merged together using spaces, unless
- the line is empty
- the line ends with a `.` (end of sentence)
2021-10-18 21:03:27 +03:00
- the line is purely of at least 3 of `-` , `=` , `_` , `*` , `~` (horizontal rule)
2021-08-19 10:20:43 +03:00
- the line starts with at least one `#` followed by a space (header)
- the line starts and ends with a `|` (table)
- the line starts with `- ` (list)
2020-06-26 20:36:40 +03:00
## Tools
2020-07-17 14:03:07 +03:00
### v fmt
2020-06-26 20:36:40 +03:00
You don't need to worry about formatting your code or setting style guidelines.
2020-07-17 14:03:07 +03:00
`v fmt` takes care of that:
2020-06-26 20:36:40 +03:00
2020-11-03 03:04:14 +03:00
```shell
2020-06-26 20:36:40 +03:00
v fmt file.v
```
2020-07-17 14:03:07 +03:00
It's recommended to set up your editor, so that `v fmt -w` runs on every save.
2020-06-26 20:36:40 +03:00
A vfmt run is usually pretty cheap (takes < 30ms ) .
Always run `v fmt -w file.v` before pushing your code.
### Profiling
2020-05-01 11:04:00 +03:00
V has good support for profiling your programs: `v -profile profile.txt run file.v`
That will produce a profile.txt file, which you can then analyze.
The generated profile.txt file will have lines with 4 columns:
a) how many times a function was called
b) how much time in total a function took (in ms)
c) how much time on average, a call to a function took (in ns)
d) the name of the v function
You can sort on column 3 (average time per function) using:
`sort -n -k3 profile.txt|tail`
2020-05-07 13:30:41 +03:00
You can also use stopwatches to measure just portions of your code explicitly:
2020-05-01 11:04:00 +03:00
```v
import time
2020-12-06 00:54:41 +03:00
fn main() {
2021-07-20 11:17:08 +03:00
sw := time.new_stopwatch()
2020-12-06 00:54:41 +03:00
println('Hello world')
println('Greeting the world took: ${sw.elapsed().nanoseconds()}ns')
2020-05-01 11:04:00 +03:00
}
```
2019-11-21 04:36:55 +03:00
2021-10-31 12:32:31 +03:00
## Package management
```powershell
v [module option] [param]
```
###### module options:
```
install Install a module from VPM.
remove Remove a module that was installed from VPM.
search Search for a module from VPM.
update Update an installed module from VPM.
upgrade Upgrade all the outdated modules.
list List all installed modules.
outdated Show installed modules that need updates.
```
You can install modules already created by someone else with [VPM ](https://vpm.vlang.io/ ):
```powershell
v install [module]
```
**Example:**
```powershell
v install ui
```
Modules can be installed directly from git or mercurial repositories.
```powershell
v install [--git|--hg] [url]
```
**Example:**
```powershell
v install --git https://github.com/vlang/markdown
```
Removing a module with v:
```powershell
v remove [module]
```
**Example:**
```powershell
v remove ui
```
Updating an installed module from [VPM ](https://vpm.vlang.io/ ):
```powershell
v update [module]
```
**Example:**
```powershell
v update ui
```
Or you can update all your modules:
```powershell
v update
```
To see all the modules you have installed, you can use:
```powershell
v list
```
**Example:**
```powershell
> v list
Installed modules:
markdown
ui
```
To see all the modules that need updates:
```powershell
v outdated
```
**Example:**
```powershell
> v outdated
Modules are up to date.
```
### Publish package
1. Put a `v.mod` file inside the toplevel folder of your module (if you
created your module with the command `v new mymodule` or `v init` you already have a v.mod file).
```sh
v new mymodule
Input your project description: My nice module.
Input your project version: (0.0.0) 0.0.1
Input your project license: (MIT)
Initialising ...
Complete!
```
Example `v.mod` :
```v ignore
Module {
name: 'mymodule'
description: 'My nice module.'
version: '0.0.1'
license: 'MIT'
dependencies: []
}
```
Minimal file structure:
```
v.mod
mymodule.v
```
The name of your module should be used with the `module` directive
at the top of all files in your module. For `mymodule.v` :
```v
module mymodule
pub fn hello_world() {
println('Hello World!')
}
```
2. Create a git repository in the folder with the `v.mod` file
(this is not required if you used `v new` or `v init` ):
```sh
git init
git add .
git commit -m "INIT"
````
3. Create a public repository on github.com.
4. Connect your local repository to the remote repository and push the changes.
5. Add your module to the public V module registry VPM:
https://vpm.vlang.io/new
You will have to login with your Github account to register the module.
2021-11-01 15:41:07 +03:00
**Warning:** _Currently it is not possible to edit your entry after submitting.
2021-10-31 12:32:31 +03:00
Check your module name and github url twice as this cannot be changed by you later._
6. The final module name is a combination of your github account and
the module name you provided e.g. `mygithubname.mymodule` .
**Optional:** tag your V module with `vlang` and `vlang-module` on github.com
to allow for a better search experience.
2020-02-09 11:32:36 +03:00
# Advanced Topics
2019-11-21 04:36:55 +03:00
2021-04-12 12:01:29 +03:00
## Dumping expressions at runtime
You can dump/trace the value of any V expression using `dump(expr)` .
For example, save this code sample as `factorial.v` , then run it with
`v run factorial.v` :
```v
fn factorial(n u32) u32 {
if dump(n < = 1) {
return dump(1)
}
return dump(n * factorial(n - 1))
}
fn main() {
println(factorial(5))
}
```
You will get:
```
[factorial.v:2] n < = 1: false
[factorial.v:2] n < = 1: false
[factorial.v:2] n < = 1: false
[factorial.v:2] n < = 1: false
[factorial.v:2] n < = 1: true
[factorial.v:3] 1: 1
[factorial.v:5] n * factorial(n - 1): 2
[factorial.v:5] n * factorial(n - 1): 6
[factorial.v:5] n * factorial(n - 1): 24
[factorial.v:5] n * factorial(n - 1): 120
120
```
2021-04-29 02:17:37 +03:00
Note that `dump(expr)` will trace both the source location,
2021-04-12 12:01:29 +03:00
the expression itself, and the expression value.
2020-07-23 01:36:23 +03:00
## Memory-unsafe code
Sometimes for efficiency you may want to write low-level code that can potentially
2020-08-12 20:14:13 +03:00
corrupt memory or be vulnerable to security exploits. V supports writing such code,
2020-07-23 01:36:23 +03:00
but not by default.
V requires that any potentially memory-unsafe operations are marked intentionally.
Marking them also indicates to anyone reading the code that there could be
memory-safety violations if there was a mistake.
Examples of potentially memory-unsafe operations are:
* Pointer arithmetic
* Pointer indexing
* Conversion to pointer from an incompatible type
* Calling certain C functions, e.g. `free` , `strlen` and `strncmp` .
To mark potentially memory-unsafe operations, enclose them in an `unsafe` block:
2020-12-09 21:58:02 +03:00
```v wip
2020-07-23 01:36:23 +03:00
// allocate 2 uninitialized bytes & return a reference to them
2020-11-03 03:04:14 +03:00
mut p := unsafe { malloc(2) }
2020-07-23 01:36:23 +03:00
p[0] = `h` // Error: pointer indexing is only allowed in `unsafe` blocks
unsafe {
2020-09-19 19:18:36 +03:00
p[0] = `h` // OK
2020-07-23 01:36:23 +03:00
p[1] = `i`
}
p++ // Error: pointer arithmetic is only allowed in `unsafe` blocks
unsafe {
p++ // OK
}
assert *p == `i`
```
2020-07-29 22:41:47 +03:00
Best practice is to avoid putting memory-safe expressions inside an `unsafe` block,
2020-08-12 20:14:13 +03:00
so that the reason for using `unsafe` is as clear as possible. Generally any code
you think is memory-safe should not be inside an `unsafe` block, so the compiler
2020-07-29 22:41:47 +03:00
can verify it.
2020-07-23 01:36:23 +03:00
2020-08-12 20:14:13 +03:00
If you suspect your program does violate memory-safety, you have a head start on
finding the cause: look at the `unsafe` blocks (and how they interact with
2020-07-23 01:36:23 +03:00
surrounding code).
* Note: This is work in progress.
2021-01-08 14:46:01 +03:00
### Structs with reference fields
Structs with references require explicitly setting the initial value to a
reference value unless the struct already defines its own initial value.
Zero-value references, or nil pointers, will **NOT** be supported in the future,
for now data structures such as Linked Lists or Binary Trees that rely on reference
fields that can use the value `0` , understanding that it is unsafe, and that it can
cause a panic.
```v
struct Node {
a & Node
b & Node = 0 // Auto-initialized to nil, use with caution!
}
// Reference fields must be initialized unless an initial value is declared.
// Zero (0) is OK but use with caution, it's a nil pointer.
foo := Node{
a: 0
}
bar := Node{
a: & foo
}
baz := Node{
a: 0
b: 0
}
qux := Node{
a: & foo
b: & bar
}
println(baz)
println(qux)
```
2021-01-30 14:57:09 +03:00
## sizeof and __offsetof
2021-02-02 20:51:40 +03:00
* `sizeof(Type)` gives the size of a type in bytes.
* `__offsetof(Struct, field_name)` gives the offset in bytes of a struct field.
2021-01-30 14:57:09 +03:00
```v
struct Foo {
a int
b int
}
2021-02-02 20:51:40 +03:00
assert sizeof(Foo) == 8
assert __offsetof(Foo, a) == 0
assert __offsetof(Foo, b) == 4
2021-01-30 14:57:09 +03:00
```
2021-02-26 09:28:37 +03:00
## Calling C from V
### Example
2019-11-21 04:36:55 +03:00
```v
#flag -lsqlite3
#include "sqlite3.h"
2020-07-11 10:49:42 +03:00
// See also the example from https://www.sqlite.org/quickstart.html
2020-12-06 00:54:41 +03:00
struct C.sqlite3 {
}
struct C.sqlite3_stmt {
}
2020-07-11 10:49:42 +03:00
2021-04-05 08:02:37 +03:00
type FnSqlite3Callback = fn (voidptr, int, & & char, & & char) int
2020-07-11 10:49:42 +03:00
2021-04-05 08:02:37 +03:00
fn C.sqlite3_open(& char, & & C.sqlite3) int
2020-12-06 00:54:41 +03:00
2020-07-11 10:49:42 +03:00
fn C.sqlite3_close(& C.sqlite3) int
2020-12-06 00:54:41 +03:00
2020-07-11 10:49:42 +03:00
fn C.sqlite3_column_int(stmt & C.sqlite3_stmt, n int) int
2020-12-06 00:54:41 +03:00
2020-12-18 23:31:49 +03:00
// ... you can also just define the type of parameter and leave out the C. prefix
2021-04-05 08:02:37 +03:00
fn C.sqlite3_prepare_v2(& C.sqlite3, & char, int, & & C.sqlite3_stmt, & & char) int
2020-12-06 00:54:41 +03:00
2021-03-05 17:41:11 +03:00
fn C.sqlite3_step(& C.sqlite3_stmt)
2020-12-06 00:54:41 +03:00
2021-03-05 17:41:11 +03:00
fn C.sqlite3_finalize(& C.sqlite3_stmt)
2020-12-06 00:54:41 +03:00
2021-04-05 08:02:37 +03:00
fn C.sqlite3_exec(db & C.sqlite3, sql & char, cb FnSqlite3Callback, cb_arg voidptr, emsg & & char) int
2020-12-06 00:54:41 +03:00
2020-07-11 10:49:42 +03:00
fn C.sqlite3_free(voidptr)
2021-04-05 08:02:37 +03:00
fn my_callback(arg voidptr, howmany int, cvalues & & char, cnames & & char) int {
2020-12-18 23:31:49 +03:00
unsafe {
for i in 0 .. howmany {
print('| ${cstring_to_vstring(cnames[i])}: ${cstring_to_vstring(cvalues[i]):20} ')
}
2020-07-11 10:49:42 +03:00
}
2020-12-06 00:54:41 +03:00
println('|')
return 0
2020-07-11 10:49:42 +03:00
}
2019-11-21 04:36:55 +03:00
fn main() {
2020-12-06 00:54:41 +03:00
db := & C.sqlite3(0) // this means `sqlite3* db = 0`
// passing a string literal to a C function call results in a C string, not a V string
2021-05-20 09:17:44 +03:00
C.sqlite3_open(c'users.db', & db)
2020-12-06 00:54:41 +03:00
// C.sqlite3_open(db_path.str, & db)
query := 'select count(*) from users'
stmt := & C.sqlite3_stmt(0)
2020-12-18 23:31:49 +03:00
// NB: you can also use the `.str` field of a V string,
// to get its C style zero terminated representation
2021-04-05 08:02:37 +03:00
C.sqlite3_prepare_v2(db, & char(query.str), -1, & stmt, 0)
2020-12-06 00:54:41 +03:00
C.sqlite3_step(stmt)
nr_users := C.sqlite3_column_int(stmt, 0)
C.sqlite3_finalize(stmt)
println('There are $nr_users users in the database.')
//
2021-04-05 08:02:37 +03:00
error_msg := & char(0)
2020-12-06 00:54:41 +03:00
query_all_users := 'select * from users'
2021-04-05 08:02:37 +03:00
rc := C.sqlite3_exec(db, & char(query_all_users.str), my_callback, voidptr(7), & error_msg)
2020-12-06 00:54:41 +03:00
if rc != C.SQLITE_OK {
2021-04-05 08:02:37 +03:00
eprintln(unsafe { cstring_to_vstring(error_msg) })
2020-12-06 00:54:41 +03:00
C.sqlite3_free(error_msg)
}
C.sqlite3_close(db)
2019-11-21 04:36:55 +03:00
}
```
2021-07-31 01:44:24 +03:00
## Calling V from C
2021-07-31 02:31:15 +03:00
Since V can compile to C, calling V code from C is very easy.
2021-07-31 01:44:24 +03:00
By default all V functions have the following naming scheme in C: `[module name]__[fn_name]` .
2021-07-31 02:31:15 +03:00
For example, `fn foo() {}` in module `bar` will result in `bar__foo()` .
2021-07-31 01:44:24 +03:00
To use a custom export name, use the `[export]` attribute:
```
[export: 'my_custom_c_name']
fn foo() {
}
```
2021-07-20 08:31:20 +03:00
## Atomics
V has no special support for atomics, yet, nevertheless it's possible to treat variables as atomics
by calling C functions from V. The standard C11 atomic functions like `atomic_store()` are usually
defined with the help of macros and C compiler magic to provide a kind of *overloaded C functions* .
Since V does not support overloading functions by intention there are wrapper functions defined in
C headers named `atomic.h` that are part of the V compiler infrastructure.
There are dedicated wrappers for all unsigned integer types and for pointers.
(`byte` is not fully supported on Windows) – the function names include the type name
as suffix. e.g. `C.atomic_load_ptr()` or `C.atomic_fetch_add_u64()` .
To use these functions the C header for the used OS has to be included and the functions
that are intended to be used have to be declared. Example:
```v globals
$if windows {
#include "@VEXEROOT/thirdparty/stdatomic/win/atomic.h"
} $else {
#include "@VEXEROOT/thirdparty/stdatomic/nix/atomic.h"
}
// declare functions we want to use - V does not parse the C header
fn C.atomic_store_u32(& u32, u32)
fn C.atomic_load_u32(& u32) u32
fn C.atomic_compare_exchange_weak_u32(& u32, & u32, u32) bool
fn C.atomic_compare_exchange_strong_u32(& u32, & u32, u32) bool
const num_iterations = 10000000
2021-07-29 10:57:31 +03:00
// see section "Global Variables" below
2021-07-20 08:31:20 +03:00
__global (
atom u32 // ordinary variable but used as atomic
)
fn change() int {
mut races_won_by_change := 0
for {
mut cmp := u32(17) // addressable value to compare with and to store the found value
// atomic version of `if atom == 17 { atom = 23 races_won_by_change++ } else { cmp = atom }`
if C.atomic_compare_exchange_strong_u32(& atom, & cmp, 23) {
races_won_by_change++
} else {
if cmp == 31 {
break
}
cmp = 17 // re-assign because overwritten with value of atom
}
}
return races_won_by_change
}
fn main() {
C.atomic_store_u32(& atom, 17)
t := go change()
mut races_won_by_main := 0
mut cmp17 := u32(17)
mut cmp23 := u32(23)
for i in 0 .. num_iterations {
// atomic version of `if atom == 17 { atom = 23 races_won_by_main++ }`
if C.atomic_compare_exchange_strong_u32(& atom, & cmp17, 23) {
races_won_by_main++
} else {
cmp17 = 17
}
desir := if i == num_iterations - 1 { u32(31) } else { u32(17) }
// atomic version of `for atom != 23 {} atom = desir`
for !C.atomic_compare_exchange_weak_u32(& atom, & cmp23, desir) {
cmp23 = 23
}
}
races_won_by_change := t.wait()
atom_new := C.atomic_load_u32(& atom)
println('atom: $atom_new, #exchanges: ${races_won_by_main + races_won_by_change}')
// prints `atom: 31, #exchanges: 10000000` )
println('races won by\n- `main()` : $races_won_by_main\n- `change()` : $races_won_by_change')
}
```
In this example both `main()` and the spawned thread `change()` try to replace a value of `17`
in the global `atom` with a value of `23` . The replacement in the opposite direction is
done exactly 10000000 times. The last replacement will be with `31` which makes the spawned
thread finish.
It is not predictable how many replacements occur in which thread, but the sum will always
be 10000000. (With the non-atomic commands from the comments the value will be higher or the program
will hang – dependent on the compiler optimization used.)
2021-07-29 10:57:31 +03:00
## Global Variables
By default V does not allow global variables. However, in low level applications they have their
place so their usage can be enabled with the compiler flag `-enable-globals` .
Declarations of global variables must be surrounded with a `__global ( ... )`
specification – as in the example [above ](#atomics ).
An initializer for global variables must be explicitly converted to the
desired target type. If no initializer is given a default initialization is done.
Some objects like semaphores and mutexes require an explicit initialization *in place* , i.e.
not with a value returned from a function call but with a method call by reference.
A separate `init()` function can be used for this purpose – it will be called before `main()` :
```v globals
import sync
__global (
sem sync.Semaphore // needs initialization in `init()`
mtx sync.RwMutex // needs initialization in `init()`
f1 = f64(34.0625) // explicily initialized
shmap shared map[string]f64 // initialized as empty `shared` map
f2 f64 // initialized to `0.0`
)
fn init() {
sem.init(0)
mtx.init()
}
```
Be aware that in multi threaded applications the access to global variables is subject
to race conditions. There are several approaches to deal with these:
- use `shared` types for the variable declarations and use `lock` blocks for access.
This is most appropriate for larger objects like structs, arrays or maps.
- handle primitive data types as "atomics" using special C-functions (see [above ](#atomics )).
- use explicit synchronization primitives like mutexes to control access. The compiler
cannot really help in this case, so you have to know what you are doing.
- don't care – this approach is possible but makes only sense if the exact values
of global variables do not really matter. An example can be found in the `rand` module
where global variables are used to generate (non cryptographic) pseudo random numbers.
In this case data races lead to random numbers in different threads becoming somewhat
correlated, which is acceptable considering the performance penalty that using
synchonization primitives would represent.
2021-02-26 09:28:37 +03:00
### Passing C compilation flags
2020-07-14 20:24:59 +03:00
2020-02-08 19:00:35 +03:00
Add `#flag` directives to the top of your V files to provide C compilation flags like:
2019-11-21 04:36:55 +03:00
2020-02-08 19:00:35 +03:00
- `-I` for adding C include files search paths
- `-l` for adding C library names that you want to get linked
- `-L` for adding C library files search paths
- `-D` for setting compile time variables
2019-11-21 04:36:55 +03:00
2021-02-26 09:28:37 +03:00
You can (optionally) use different flags for different targets.
2020-10-20 21:14:56 +03:00
Currently the `linux` , `darwin` , `freebsd` , and `windows` flags are supported.
2020-02-08 19:00:35 +03:00
2020-05-07 13:30:41 +03:00
NB: Each flag must go on its own line (for now)
2019-11-21 04:36:55 +03:00
2020-11-03 03:04:14 +03:00
```v oksyntax
2019-11-21 04:36:55 +03:00
#flag linux -lsdl2
#flag linux -Ivig
#flag linux -DCIMGUI_DEFINE_ENUMS_AND_STRUCTS=1
#flag linux -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS=1
#flag linux -DIMGUI_IMPL_API=
```
2021-03-21 09:43:12 +03:00
In the console build command, you can use:
* `-cflags` to pass custom flags to the backend C compiler.
* `-cc` to change the default C backend compiler.
* For example: `-cc gcc-9 -cflags -fsanitize=thread` .
2021-02-26 09:28:37 +03:00
2021-03-21 09:43:12 +03:00
You can define a `VFLAGS` environment variable in your terminal to store your `-cc`
and `-cflags` settings, rather than including them in the build command each time.
2021-02-26 09:28:37 +03:00
2020-10-29 10:02:49 +03:00
### #pkgconfig
Add `#pkgconfig` directive is used to tell the compiler which modules should be used for compiling
and linking using the pkg-config files provided by the respective dependencies.
As long as backticks can't be used in `#flag` and spawning processes is not desirable for security
and portability reasons, V uses its own pkgconfig library that is compatible with the standard
freedesktop one.
If no flags are passed it will add `--cflags` and `--libs` , both lines below do the same:
2020-11-03 03:04:14 +03:00
```v oksyntax
2020-10-29 10:02:49 +03:00
#pkgconfig r_core
#pkgconfig --cflags --libs r_core
```
The `.pc` files are looked up into a hardcoded list of default pkg-config paths, the user can add
extra paths by using the `PKG_CONFIG_PATH` environment variable. Multiple modules can be passed.
2021-10-31 15:12:04 +03:00
To check the existence of a pkg-config use `$pkgconfig('pkg')` as a compile time "if" condition to
2021-07-09 18:17:04 +03:00
check if a pkg-config exists. If it exists the branch will be created. Use `$else` or `$else $if`
to handle other cases.
```v ignore
$if $pkgconfig('mysqlclient') {
#pkgconfig mysqlclient
} $else $if $pkgconfig('mariadb') {
#pkgconfig mariadb
}
```
2020-07-14 20:24:59 +03:00
### Including C code
2020-10-20 21:14:56 +03:00
You can also include C code directly in your V module.
For example, let's say that your C code is located in a folder named 'c' inside your module folder.
Then:
2020-02-08 19:00:35 +03:00
2020-02-29 16:23:45 +03:00
* Put a v.mod file inside the toplevel folder of your module (if you
2020-06-14 21:20:26 +03:00
created your module with `v new` you already have v.mod file). For
2020-02-29 16:23:45 +03:00
example:
2020-11-03 03:04:14 +03:00
```v ignore
2020-02-29 16:23:45 +03:00
Module {
name: 'mymodule',
description: 'My nice module wraps a simple C library.',
version: '0.0.1'
dependencies: []
}
```
2020-03-01 17:49:39 +03:00
2020-02-29 16:23:45 +03:00
* Add these lines to the top of your module:
2020-11-03 03:04:14 +03:00
```v oksyntax
2021-04-19 19:01:47 +03:00
#flag -I @VMODROOT/c
#flag @VMODROOT/c/implementation.o
2020-02-08 19:00:35 +03:00
#include "header.h"
```
2021-04-19 19:01:47 +03:00
NB: @VMODROOT will be replaced by V with the *nearest parent folder, where there is a v.mod file* .
2020-10-20 21:14:56 +03:00
Any .v file beside or below the folder where the v.mod file is,
2021-04-19 19:01:47 +03:00
can use `#flag @VMODROOT/abc` to refer to this folder.
The @VMODROOT folder is also *prepended* to the module lookup path,
so you can *import* other modules under your @VMODROOT , by just naming them.
2020-02-08 19:00:35 +03:00
2020-10-20 21:14:56 +03:00
The instructions above will make V look for an compiled .o file in
your module `folder/c/implementation.o` .
2020-02-08 19:00:35 +03:00
If V finds it, the .o file will get linked to the main executable, that used the module.
2021-04-19 19:01:47 +03:00
If it does not find it, V assumes that there is a `@VMODROOT/c/implementation.c` file,
2020-02-08 19:00:35 +03:00
and tries to compile it to a .o file, then will use that.
2020-02-29 16:23:45 +03:00
2020-02-08 19:00:35 +03:00
This allows you to have C code, that is contained in a V module, so that its distribution is easier.
2020-07-14 20:24:59 +03:00
You can see a complete minimal example for using C code in a V wrapper module here:
[project_with_c_code ](https://github.com/vlang/v/tree/master/vlib/v/tests/project_with_c_code ).
2020-10-20 17:38:29 +03:00
Another example, demonstrating passing structs from C to V and back again:
[interoperate between C to V to C ](https://github.com/vlang/v/tree/master/vlib/v/tests/project_with_c_code_2 ).
2020-02-08 19:00:35 +03:00
2020-07-14 20:24:59 +03:00
### C types
2020-12-18 19:18:16 +03:00
Ordinary zero terminated C strings can be converted to V strings with
2021-04-12 15:58:26 +03:00
`unsafe { &char(cstring).vstring() }` or if you know their length already with
`unsafe { &char(cstring).vstring_with_len(len) }` .
2019-11-21 04:36:55 +03:00
2020-12-18 19:18:16 +03:00
NB: The .vstring() and .vstring_with_len() methods do NOT create a copy of the `cstring` ,
so you should NOT free it after calling the method `.vstring()` .
2020-10-20 21:14:56 +03:00
If you need to make a copy of the C string (some libc APIs like `getenv` pretty much require that,
since they return pointers to internal libc memory), you can use `cstring_to_vstring(cstring)` .
2020-02-08 19:00:35 +03:00
On Windows, C APIs often return so called `wide` strings (utf16 encoding).
These can be converted to V strings with `string_from_wide(&u16(cwidestring))` .
V has these types for easier interoperability with C:
- `voidptr` for C's `void*` ,
2021-04-12 15:58:26 +03:00
- `&byte` for C's `byte*` and
- `&char` for C's `char*` .
- `&&char` for C's `char**`
2020-02-08 19:00:35 +03:00
2020-05-07 13:30:41 +03:00
To cast a `voidptr` to a V reference, use `user := &User(user_void_ptr)` .
2019-11-21 04:36:55 +03:00
2020-05-07 13:30:41 +03:00
`voidptr` can also be dereferenced into a V struct through casting: `user := User(user_void_ptr)` .
2019-11-21 04:36:55 +03:00
2021-02-06 09:12:25 +03:00
[an example of a module that calls C code from V ](https://github.com/vlang/v/blob/master/vlib/v/tests/project_with_c_code/mod1/wrapper.v )
2020-07-14 20:24:59 +03:00
2021-03-21 09:43:12 +03:00
### C Declarations
2021-04-29 02:17:37 +03:00
C identifiers are accessed with the `C` prefix similarly to how module-specific
identifiers are accessed. Functions must be redeclared in V before they can be used.
Any C types may be used behind the `C` prefix, but types must be redeclared in V in
2021-03-21 09:43:12 +03:00
order to access type members.
To redeclare complex types, such as in the following C code:
```c
struct SomeCStruct {
uint8_t implTraits;
uint16_t memPoolData;
union {
struct {
void* data;
size_t size;
};
DataView view;
};
};
```
members of sub-data-structures may be directly declared in the containing struct as below:
```v
struct C.SomeCStruct {
implTraits byte
memPoolData u16
// These members are part of sub data structures that can't currently be represented in V.
// Declaring them directly like this is sufficient for access.
// union {
// struct {
data voidptr
2021-09-08 13:09:32 +03:00
size usize
2021-03-21 09:43:12 +03:00
// }
view C.DataView
// }
}
```
2021-04-29 02:17:37 +03:00
The existence of the data members is made known to V, and they may be used without
2021-03-21 09:43:12 +03:00
re-creating the original structure exactly.
2021-04-29 02:17:37 +03:00
Alternatively, you may [embed ](#embedded-structs ) the sub-data-structures to maintain
2021-03-21 09:43:12 +03:00
a parallel code structure.
2021-06-04 17:55:22 +03:00
## Debugging
2020-02-08 19:00:35 +03:00
2021-06-25 12:11:29 +03:00
### C Backend binaries (Default)
2021-06-04 17:55:22 +03:00
To debug issues in the generated binary (flag: `-b c` ), you can pass these flags:
2020-02-08 19:00:35 +03:00
2021-01-13 13:50:35 +03:00
- `-g` - produces a less optimized executable with more debug information in it.
V will enforce line numbers from the .v files in the stacktraces, that the
executable will produce on panic. It is usually better to pass -g, unless
you are writing low level code, in which case use the next option `-cg` .
2020-02-08 19:00:35 +03:00
- `-cg` - produces a less optimized executable with more debug information in it.
2021-01-13 13:50:35 +03:00
The executable will use C source line numbers in this case. It is frequently
used in combination with `-keepc` , so that you can inspect the generated
C program in case of panic, or so that your debugger (`gdb`, `lldb` etc.)
can show you the generated C source code.
2020-05-02 19:01:43 +03:00
- `-showcc` - prints the C command that is used to build the program.
2021-01-13 13:50:35 +03:00
- `-show-c-output` - prints the output, that your C compiler produced
while compiling your program.
- `-keepc` - do not delete the generated C source code file after a successful
compilation. Also keep using the same file path, so it is more stable,
and easier to keep opened in an editor/IDE.
2019-11-21 04:36:55 +03:00
2021-01-13 13:50:35 +03:00
For best debugging experience if you are writing a low level wrapper for an existing
C library, you can pass several of these flags at the same time:
`v -keepc -cg -showcc yourprogram.v` , then just run your debugger (gdb/lldb) or IDE
on the produced executable `yourprogram` .
2020-02-08 19:00:35 +03:00
2020-10-20 21:14:56 +03:00
If you just want to inspect the generated C code,
without further compilation, you can also use the `-o` flag (e.g. `-o file.c` ).
This will make V produce the `file.c` then stop.
2019-11-21 04:36:55 +03:00
2020-10-20 21:14:56 +03:00
If you want to see the generated C source code for *just* a single C function,
for example `main` , you can use: `-printfn main -o file.c` .
2020-05-02 19:01:43 +03:00
2021-06-07 11:09:07 +03:00
To debug the V executable itself you need to compile from src with `./v -g -o v cmd/v` .
2021-06-25 12:11:29 +03:00
You can debug tests with for example `v -g -keepc prog_test.v` . The `-keepc` flag is needed,
2021-06-07 11:09:07 +03:00
so that the executable is not deleted, after it was created and ran.
2020-10-20 21:14:56 +03:00
To see a detailed list of all flags that V supports,
use `v help` , `v help build` and `v help build-c` .
2020-05-02 19:01:43 +03:00
2021-06-04 17:55:22 +03:00
**Commandline Debugging**
1. compile your binary with debugging info `v -g hello.v`
2. debug with [lldb ](https://lldb.llvm.org ) or [GDB ](https://www.gnu.org/software/gdb/ ) e.g. `lldb hello`
2021-10-18 21:03:27 +03:00
[Troubleshooting (debugging) executables created with V in GDB ](https://github.com/vlang/v/wiki/Troubleshooting-(debugging )-executables-created-with-V-in-GDB)
2021-06-04 17:55:22 +03:00
**Visual debugging Setup:**
* [Visual Studio Code ](vscode.md )
### Native Backend binaries
2021-06-04 18:17:34 +03:00
Currently there is no debugging support for binaries, created by the
native backend (flag: `-b native` ).
2021-06-04 17:55:22 +03:00
### Javascript Backend
2021-10-18 21:03:27 +03:00
To debug the generated Javascript output you can activate source maps:
2021-06-15 04:43:51 +03:00
`v -b js -sourcemap hello.v -o hello.js`
2021-06-04 17:55:22 +03:00
2021-06-15 04:43:51 +03:00
For all supported options check the latest help:
`v help build-js`
2021-06-04 17:55:22 +03:00
2020-05-07 13:30:41 +03:00
## Conditional compilation
2019-11-21 04:36:55 +03:00
2021-01-14 19:19:04 +03:00
### Compile time code
2020-12-25 23:41:22 +03:00
2021-01-23 11:28:12 +03:00
`$` is used as a prefix for compile-time operations.
2021-06-14 13:12:02 +03:00
#### `$if` condition
2019-11-21 04:36:55 +03:00
```v
2020-09-19 00:48:59 +03:00
// Support for multiple conditions in one branch
$if ios || android {
2020-12-06 00:54:41 +03:00
println('Running on a mobile device!')
2019-11-21 04:36:55 +03:00
}
2020-09-19 00:48:59 +03:00
$if linux & & x64 {
2020-12-06 00:54:41 +03:00
println('64-bit Linux.')
2019-11-21 04:36:55 +03:00
}
2020-09-19 00:48:59 +03:00
// Usage as expression
os := $if windows { 'Windows' } $else { 'UNIX' }
println('Using $os')
// $else-$if branches
$if tinyc {
2020-12-06 00:54:41 +03:00
println('tinyc')
2020-09-19 00:48:59 +03:00
} $else $if clang {
2020-12-06 00:54:41 +03:00
println('clang')
2020-09-19 00:48:59 +03:00
} $else $if gcc {
2020-12-06 00:54:41 +03:00
println('gcc')
2020-09-19 00:48:59 +03:00
} $else {
2020-12-06 00:54:41 +03:00
println('different compiler')
2019-11-21 04:36:55 +03:00
}
2020-09-19 00:48:59 +03:00
$if test {
2020-12-06 00:54:41 +03:00
println('testing')
2020-08-20 09:33:08 +03:00
}
2020-09-19 00:48:59 +03:00
// v -cg ...
2019-11-21 04:36:55 +03:00
$if debug {
2020-12-06 00:54:41 +03:00
println('debugging')
2019-11-21 04:36:55 +03:00
}
2021-03-10 20:26:34 +03:00
// v -prod ...
$if prod {
println('production build')
}
2020-09-19 00:48:59 +03:00
// v -d option ...
$if option ? {
2020-12-06 00:54:41 +03:00
println('custom option')
2020-09-19 00:48:59 +03:00
}
2019-11-21 04:36:55 +03:00
```
2020-09-25 13:02:32 +03:00
If you want an `if` to be evaluated at compile time it must be prefixed with a `$` sign.
2020-09-19 00:48:59 +03:00
Right now it can be used to detect an OS, compiler, platform or compilation options.
`$if debug` is a special option like `$if windows` or `$if x32` .
If you're using a custom ifdef, then you do need `$if option ? {}` and compile with`v -d option`.
Full list of builtin options:
2021-03-10 20:26:34 +03:00
| OS | Compilers | Platforms | Other |
| --- | --- | --- | --- |
2021-04-26 19:01:42 +03:00
| `windows` , `linux` , `macos` | `gcc` , `tinyc` | `amd64` , `arm64` | `debug` , `prod` , `test` |
2021-03-10 20:26:34 +03:00
| `mac` , `darwin` , `ios` , | `clang` , `mingw` | `x64` , `x32` | `js` , `glibc` , `prealloc` |
2021-04-09 23:24:25 +03:00
| `android` ,`mach`, `dragonfly` | `msvc` | `little_endian` | `no_bounds_checking` , `freestanding` |
| `gnu` , `hpux` , `haiku` , `qnx` | `cplusplus` | `big_endian` |
2021-05-08 21:00:21 +03:00
| `solaris` | | | |
2019-11-21 04:36:55 +03:00
2021-06-14 13:12:02 +03:00
#### `$embed_file`
2021-01-14 19:19:04 +03:00
```v ignore
2021-03-20 10:30:38 +03:00
import os
2021-01-14 19:19:04 +03:00
fn main() {
embedded_file := $embed_file('v.png')
2021-03-20 10:30:38 +03:00
os.write_file('exported.png', embedded_file.to_string()) ?
2021-01-14 19:19:04 +03:00
}
```
V can embed arbitrary files into the executable with the `$embed_file(<path>)`
compile time call. Paths can be absolute or relative to the source file.
When you do not use `-prod` , the file will not be embedded. Instead, it will
2021-10-31 12:32:31 +03:00
be loaded *the first time* your program calls `embedded_file.data()` at runtime, making
2021-01-14 19:19:04 +03:00
it easier to change in external editor programs, without needing to recompile
your executable.
When you compile with `-prod` , the file *will be embedded inside* your
2021-01-20 11:30:26 +03:00
executable, increasing your binary size, but making it more self contained
2021-10-31 12:32:31 +03:00
and thus easier to distribute. In this case, `embedded_file.data()` will cause *no IO* ,
2021-01-14 19:19:04 +03:00
and it will always return the same data.
2021-06-14 13:12:02 +03:00
#### `$tmpl` for embedding and parsing V template files
2021-02-01 17:45:45 +03:00
V has a simple template language for text and html templates, and they can easily
be embedded via `$tmpl('path/to/template.txt')` :
```v ignore
fn build() string {
name := 'Peter'
age := 25
numbers := [1, 2, 3]
return $tmpl('1.txt')
}
fn main() {
println(build())
}
```
1.txt:
```
name: @name
age: @age
numbers: @numbers
@for number in numbers
@number
@end
```
output:
```
name: Peter
age: 25
numbers: [1, 2, 3]
1
2
3
```
2021-06-14 13:12:02 +03:00
#### `$env`
2021-01-31 20:22:42 +03:00
```v
module main
fn main() {
compile_time_env := $env('ENV_VAR')
println(compile_time_env)
}
```
V can bring in values at compile time from environment variables.
`$env('ENV_VAR')` can also be used in top-level `#flag` and `#include` statements:
`#flag linux -I $env('JAVA_HOME')/include` .
2020-12-25 23:41:22 +03:00
### Environment specific files
If a file has an environment-specific suffix, it will only be compiled for that environment.
- `.js.v` => will be used only by the JS backend. These files can contain JS. code.
- `.c.v` => will be used only by the C backend. These files can contain C. code.
2021-04-26 16:39:38 +03:00
- `.native.v` => will be used only by V's native backend.
2020-12-25 23:41:22 +03:00
- `_nix.c.v` => will be used only on Unix systems (non Windows).
2020-12-30 04:27:05 +03:00
- `_${os}.c.v` => will be used only on the specific `os` system.
2020-12-25 23:41:22 +03:00
For example, `_windows.c.v` will be used only when compiling on Windows, or with `-os windows` .
2020-12-30 04:27:05 +03:00
- `_default.c.v` => will be used only if there is NOT a more specific platform file.
For example, if you have both `file_linux.c.v` and `file_default.c.v` ,
and you are compiling for linux, then only `file_linux.c.v` will be used,
2020-12-25 23:41:22 +03:00
and `file_default.c.v` will be ignored.
Here is a more complete example:
main.v:
```v ignore
module main
fn main() { println(message) }
```
main_default.c.v:
```v ignore
module main
const ( message = 'Hello world' )
```
main_linux.c.v:
```v ignore
module main
const ( message = 'Hello linux' )
```
main_windows.c.v:
```v ignore
module main
const ( message = 'Hello windows' )
```
With the example above:
- when you compile for windows, you will get 'Hello windows'
- when you compile for linux, you will get 'Hello linux'
2020-12-30 04:27:05 +03:00
- when you compile for any other platform, you will get the
2020-12-25 23:41:22 +03:00
non specific 'Hello world' message.
2021-01-30 19:03:15 +03:00
- `_d_customflag.v` => will be used *only* if you pass `-d customflag` to V.
That corresponds to `$if customflag ? {}` , but for a whole file, not just a
single block. `customflag` should be a snake_case identifier, it can not
contain arbitrary characters (only lower case latin letters + numbers + `_` ).
NB: a combinatorial `_d_customflag_linux.c.v` postfix will not work.
If you do need a custom flag file, that has platform dependent code, use the
2021-02-01 16:50:41 +03:00
postfix `_d_customflag.v` , and then use plaftorm dependent compile time
2021-01-30 19:03:15 +03:00
conditional blocks inside it, i.e. `$if linux {}` etc.
2021-04-29 02:17:37 +03:00
- `_notd_customflag.v` => similar to _d_customflag.v, but will be used
2021-03-20 16:16:36 +03:00
*only* if you do NOT pass `-d customflag` to V.
2020-05-26 23:39:15 +03:00
## Compile time pseudo variables
2020-10-20 21:14:56 +03:00
V also gives your code access to a set of pseudo string variables,
that are substituted at compile time:
2020-05-26 23:39:15 +03:00
- `@FN` => replaced with the name of the current V function
2021-02-05 17:30:58 +03:00
- `@METHOD` => replaced with ReceiverType.MethodName
2020-05-26 23:39:15 +03:00
- `@MOD` => replaced with the name of the current V module
- `@STRUCT` => replaced with the name of the current V struct
- `@FILE` => replaced with the path of the V source file
- `@LINE` => replaced with the V line number where it appears (as a string).
- `@COLUMN` => replaced with the column where it appears (as a string).
- `@VEXE` => replaced with the path to the V compiler
2021-04-19 19:01:47 +03:00
- `@VEXEROOT` => will be substituted with the *folder* ,
where the V executable is (as a string).
2020-05-26 23:39:15 +03:00
- `@VHASH` => replaced with the shortened commit hash of the V compiler (as a string).
- `@VMOD_FILE` => replaced with the contents of the nearest v.mod file (as a string).
2021-04-19 19:01:47 +03:00
- `@VMODROOT` => will be substituted with the *folder* ,
where the nearest v.mod file is (as a string).
2020-05-26 23:39:15 +03:00
That allows you to do the following example, useful while debugging/logging/tracing your code:
```v
2020-12-06 00:54:41 +03:00
eprintln('file: ' + @FILE + ' | line: ' + @LINE + ' | fn: ' + @MOD + '.' + @FN )
2020-05-26 23:39:15 +03:00
```
Another example, is if you want to embed the version/name from v.mod *inside* your executable:
2020-11-03 03:04:14 +03:00
```v ignore
2020-05-26 23:39:15 +03:00
import v.vmod
2021-02-28 23:20:21 +03:00
vm := vmod.decode( @VMOD_FILE ) or { panic(err.msg) }
2020-05-26 23:39:15 +03:00
eprintln('$vm.name $vm.version\n $vm.description')
```
2020-06-09 17:36:18 +03:00
## Performance tuning
2020-07-09 18:14:14 +03:00
The generated C code is usually fast enough, when you compile your code
with `-prod` . There are some situations though, where you may want to give
2020-08-24 10:04:50 +03:00
additional hints to the compiler, so that it can further optimize some
2020-06-09 19:09:41 +03:00
blocks of code.
NB: These are *rarely* needed, and should not be used, unless you
*profile your code*, and then see that there are significant benefits for them.
To cite gcc's documentation: "programmers are notoriously bad at predicting
how their programs actually perform".
`[inline]` - you can tag functions with `[inline]` , so the C compiler will
2020-07-09 18:14:14 +03:00
try to inline them, which in some cases, may be beneficial for performance,
2020-06-09 19:09:41 +03:00
but may impact the size of your executable.
2020-06-09 17:36:18 +03:00
2020-08-24 10:04:50 +03:00
`[direct_array_access]` - in functions tagged with `[direct_array_access]`
2020-09-19 00:48:59 +03:00
the compiler will translate array operations directly into C array operations -
2021-10-31 15:12:04 +03:00
omitting bounds checking. This may save a lot of time in a function that iterates
2020-08-24 10:04:50 +03:00
over an array but at the cost of making the function unsafe - unless
2020-12-23 20:03:56 +03:00
the boundaries will be checked by the user.
2020-08-24 10:04:50 +03:00
2020-07-09 18:14:14 +03:00
`if _likely_(bool expression) {` this hints the C compiler, that the passed
boolean expression is very likely to be true, so it can generate assembly
2020-06-09 17:36:18 +03:00
code, with less chance of branch misprediction. In the JS backend,
that does nothing.
2020-06-09 18:08:31 +03:00
`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.
2020-08-11 18:57:39 +03:00
< a id = 'Reflection via codegen' >
## Compile-time reflection
2019-11-21 04:36:55 +03:00
Having built-in JSON support is nice, but V also allows you to create efficient
2020-08-11 18:57:39 +03:00
serializers for any data format. V has compile-time `if` and `for` constructs:
2019-11-21 04:36:55 +03:00
2021-10-31 12:32:31 +03:00
```v
2020-08-11 18:57:39 +03:00
struct User {
2021-10-31 12:32:31 +03:00
name string
age int
2019-11-21 04:36:55 +03:00
}
2021-10-31 12:32:31 +03:00
fn main() {
$for field in User.fields {
$if field.typ is string {
println('$field.name is of type string')
}
}
2019-11-21 04:36:55 +03:00
}
2021-10-31 12:32:31 +03:00
// Output:
// name is of type string
2019-11-21 04:36:55 +03:00
```
2021-10-31 12:32:31 +03:00
See [`examples/compiletime/reflection.v` ](/examples/compiletime/reflection.v )
for a more complete example.
2019-11-21 04:36:55 +03:00
## Limited operator overloading
```v
struct Vec {
2020-12-06 00:54:41 +03:00
x int
y int
2019-11-21 04:36:55 +03:00
}
fn (a Vec) str() string {
2020-12-06 00:54:41 +03:00
return '{$a.x, $a.y}'
2019-11-21 04:36:55 +03:00
}
2020-12-21 22:20:00 +03:00
fn (a Vec) + (b Vec) Vec {
2020-12-06 00:54:41 +03:00
return Vec{a.x + b.x, a.y + b.y}
2019-11-21 04:36:55 +03:00
}
2020-12-21 22:20:00 +03:00
fn (a Vec) - (b Vec) Vec {
2020-12-06 00:54:41 +03:00
return Vec{a.x - b.x, a.y - b.y}
2019-11-21 04:36:55 +03:00
}
fn main() {
2020-12-06 00:54:41 +03:00
a := Vec{2, 3}
b := Vec{4, 5}
2021-01-13 05:31:14 +03:00
mut c := Vec{1, 2}
2020-12-06 00:54:41 +03:00
println(a + b) // "{6, 8}"
println(a - b) // "{-2, -2}"
2021-01-13 05:31:14 +03:00
c += a
println(c) // "{3, 5}"
2019-11-21 04:36:55 +03:00
}
```
2020-10-20 21:14:56 +03:00
Operator overloading goes against V's philosophy of simplicity and predictability.
But since scientific and graphical applications are among V's domains,
operator overloading is an important feature to have in order to improve readability:
2019-11-21 04:36:55 +03:00
`a.add(b).add(c.mul(d))` is a lot less readable than `a + b + c * d` .
2020-05-07 13:30:41 +03:00
To improve safety and maintainability, operator overloading is limited:
2019-11-21 04:36:55 +03:00
2021-01-10 13:24:46 +03:00
- It's only possible to overload `+, -, *, /, %, <, >, ==, !=, <=, >=` operators.
2021-10-31 15:12:04 +03:00
- `==` and `!=` are self generated by the compiler but can be overridden.
2020-02-08 19:00:35 +03:00
- Calling other functions inside operator functions is not allowed.
- Operator functions can't modify their arguments.
2021-02-03 17:18:38 +03:00
- When using `<` and `==` operators, the return type must be `bool` .
- `!=` , `>` , `<=` and `>=` are auto generated when `==` and `<` are defined.
2020-02-08 19:00:35 +03:00
- Both arguments must have the same type (just like with all operators in V).
2021-01-13 13:50:35 +03:00
- Assignment operators (`*=`, `+=` , `/=` , etc)
2021-10-31 12:32:31 +03:00
are auto generated when the corresponding operators are defined and operands are of the same type.
2019-11-21 04:36:55 +03:00
## Inline assembly
2021-04-29 02:17:37 +03:00
<!-- ignore because it doesn't pass fmt test (why?) -->
2021-03-17 03:43:17 +03:00
```v ignore
a := 100
b := 20
mut c := 0
asm amd64 {
mov eax, a
add eax, b
mov c, eax
2021-04-29 02:17:37 +03:00
; =r (c) as c // output
; r (a) as a // input
2021-03-17 03:43:17 +03:00
r (b) as b
2019-11-21 04:36:55 +03:00
}
2021-04-29 02:17:37 +03:00
println('a: $a') // 100
println('b: $b') // 20
2021-03-17 03:43:17 +03:00
println('c: $c') // 120
2019-11-21 04:36:55 +03:00
```
2021-03-17 03:43:17 +03:00
For more examples, see [github.com/vlang/v/tree/master/vlib/v/tests/assembly/asm_test.amd64.v ](https://github.com/vlang/v/tree/master/vlib/v/tests/assembly/asm_test.amd64.v )
2020-12-17 14:25:31 +03:00
## Translating C to V
2019-11-21 04:36:55 +03:00
2020-12-17 14:25:31 +03:00
TODO: translating C to V will be available in V 0.3.
2019-11-21 04:36:55 +03:00
2020-12-17 14:25:31 +03:00
V can translate your C code to human readable V code and generate V wrappers on top of C libraries.
2019-11-21 04:36:55 +03:00
2020-12-17 14:25:31 +03:00
Let's create a simple program `test.c` first:
```c
#include "stdio.h"
2019-11-21 04:36:55 +03:00
int main() {
2020-12-17 14:25:31 +03:00
for (int i = 0; i < 10 ; i + + ) {
printf("hello world\n");
}
2019-11-21 04:36:55 +03:00
return 0;
}
```
2020-12-17 14:25:31 +03:00
Run `v translate test.c` , and V will generate `test.v` :
2019-11-21 04:36:55 +03:00
```v
2020-11-03 03:04:14 +03:00
fn main() {
2020-12-17 14:25:31 +03:00
for i := 0; i < 10 ; i + + {
println('hello world')
}
2019-11-21 04:36:55 +03:00
}
```
2020-12-17 14:25:31 +03:00
To generate a wrapper on top of a C library use this command:
```bash
v wrapper c_code/libsodium/src/libsodium
```
This will generate a directory `libsodium` with a V module.
Example of a C2V generated libsodium wrapper:
2021-11-02 03:35:54 +03:00
https://github.com/vlang/libsodium
2020-12-17 14:25:31 +03:00
< br >
2019-11-21 04:36:55 +03:00
When should you translate C code and when should you simply call C code from V?
2020-10-20 21:14:56 +03:00
If you have well-written, well-tested C code,
then of course you can always simply call this C code from V.
2019-11-21 04:36:55 +03:00
Translating it to V gives you several advantages:
2020-10-20 21:14:56 +03:00
- If you plan to develop that code base, you now have everything in one language,
which is much safer and easier to develop in than C.
2020-01-16 17:29:00 +03:00
- Cross-compilation becomes a lot easier. You don't have to worry about it at all.
- No more build flags and include files either.
2019-11-21 04:36:55 +03:00
## Hot code reloading
2020-11-14 21:02:10 +03:00
```v live
2019-11-21 04:36:55 +03:00
module main
import time
[live]
fn print_message() {
2020-12-06 00:54:41 +03:00
println('Hello! Modify this message while the program is running.')
2019-11-21 04:36:55 +03:00
}
fn main() {
2020-12-06 00:54:41 +03:00
for {
print_message()
2021-02-28 19:01:31 +03:00
time.sleep(500 * time.millisecond)
2020-12-06 00:54:41 +03:00
}
2019-11-21 04:36:55 +03:00
}
```
Build this example with `v -live message.v` .
Functions that you want to be reloaded must have `[live]` attribute
before their definition.
Right now it's not possible to modify types while the program is running.
More examples, including a graphical application:
2020-02-17 14:19:15 +03:00
[github.com/vlang/v/tree/master/examples/hot_code_reload ](https://github.com/vlang/v/tree/master/examples/hot_reload ).
2019-11-21 04:36:55 +03:00
## Cross compilation
To cross compile your project simply run
2020-11-03 03:04:14 +03:00
```shell
2019-11-21 04:36:55 +03:00
v -os windows .
```
or
2020-11-03 03:04:14 +03:00
```shell
2019-11-21 04:36:55 +03:00
v -os linux .
```
(Cross compiling for macOS is temporarily not possible.)
If you don't have any C dependencies, that's all you need to do. This works even
when compiling GUI apps using the `ui` module or graphical apps using `gg` .
You will need to install Clang, LLD linker, and download a zip file with
libraries and include files for Windows and Linux. V will provide you with a link.
## Cross-platform shell scripts in V
V can be used as an alternative to Bash to write deployment scripts, build scripts, etc.
The advantage of using V for this is the simplicity and predictability of the language, and
cross-platform support. "V scripts" run on Unix-like systems as well as on Windows.
2020-03-10 16:23:55 +03:00
Use the `.vsh` file extension. It will make all functions in the `os`
2021-02-20 19:55:24 +03:00
module global (so that you can use `mkdir()` instead of `os.mkdir()` , for example).
2019-11-21 04:36:55 +03:00
2021-02-20 19:55:24 +03:00
An example `deploy.vsh` :
2020-11-03 03:04:14 +03:00
```v wip
2021-02-23 19:14:14 +03:00
#!/usr/bin/env -S v run
2020-03-10 16:23:55 +03:00
// The shebang above associates the file to V on Unix-like systems,
// so it can be run just by specifying the path to the file
// once it's made executable using `chmod +x` .
2021-02-20 19:55:24 +03:00
// Remove if build/ exits, ignore any errors if it doesn't
rmdir_all('build') or { }
// Create build/, never fails as build/ does not exist
mkdir('build') ?
// Move *.v files to build/
result := exec('mv *.v build/') ?
if result.exit_code != 0 {
println(result.output)
}
// Similar to:
// files := ls('.') ?
// mut count := 0
// if files.len > 0 {
// for file in files {
// if file.ends_with('.v') {
// mv(file, 'build/') or {
// println('err: $err')
// return
// }
// }
// count++
// }
// }
// if count == 0 {
// println('No files')
// }
2019-11-21 04:36:55 +03:00
```
Now you can either compile this like a normal V program and get an executable you can deploy and run
anywhere:
2020-03-10 16:23:55 +03:00
`v deploy.vsh && ./deploy`
Or just run it more like a traditional Bash script:
`v run deploy.vsh`
2019-11-21 04:36:55 +03:00
2020-03-10 16:23:55 +03:00
On Unix-like platforms, the file can be run directly after making it executable using `chmod +x` :
`./deploy.vsh`
2019-11-21 04:36:55 +03:00
2020-06-09 23:40:36 +03:00
## Attributes
2020-06-09 23:49:40 +03:00
V has several attributes that modify the behavior of functions and structs.
2021-02-21 12:56:34 +03:00
An attribute is a compiler instruction specified inside `[]` right before a
function/struct/enum declaration and applies only to the following declaration.
2020-06-10 00:04:46 +03:00
2020-06-09 23:40:36 +03:00
```v
2021-08-23 14:23:59 +03:00
// [flag] enables Enum types to be used as bitfields
[flag]
enum BitField {
read
write
other
}
2021-09-26 21:24:35 +03:00
fn main() {
2021-08-23 14:23:59 +03:00
assert 1 == int(BitField.read)
assert 2 == int(BitField.write)
mut bf := BitField.read
2021-09-26 21:24:35 +03:00
assert bf.has(.read | .other) // test if *at least one* of the flags is set
assert !bf.all(.read | .other) // test if *all* of the flags is set
2021-08-23 14:23:59 +03:00
bf.set(.write | .other)
assert bf.has(.read | .write | .other)
2021-09-26 21:24:35 +03:00
assert bf.all(.read | .write | .other)
bf.toggle(.other)
assert bf == BitField.read | .write
assert bf.all(.read | .write)
assert !bf.has(.other)
2021-08-23 14:23:59 +03:00
}
2021-09-26 21:24:35 +03:00
```
2021-08-23 14:23:59 +03:00
2021-09-26 21:24:35 +03:00
```v
2020-06-09 23:47:41 +03:00
// Calling this function will result in a deprecation warning
2020-06-09 23:40:36 +03:00
[deprecated]
2020-12-06 00:54:41 +03:00
fn old_function() {
}
2020-06-09 23:49:40 +03:00
2021-03-09 19:24:21 +03:00
// It can also display a custom deprecation message
[deprecated: 'use new_function() instead']
fn legacy_function() {}
2021-08-04 17:41:00 +03:00
// You can also specify a date, after which the function will be
// considered deprecated. Before that date, calls to the function
// will be compiler notices - you will see them, but the compilation
// is not affected. After that date, calls will become warnings,
// so ordinary compiling will still work, but compiling with -prod
// will not (all warnings are treated like errors with -prod).
// 6 months after the deprecation date, calls will be hard
// compiler errors.
[deprecated: 'use new_function2() instead']
[deprecated_after: '2021-05-27']
fn legacy_function2() {}
2021-09-26 21:24:35 +03:00
```
2021-08-04 17:41:00 +03:00
2021-09-26 21:24:35 +03:00
```v nofmt
2020-06-09 23:49:40 +03:00
// This function's calls will be inlined.
[inline]
2020-12-06 00:54:41 +03:00
fn inlined_function() {
}
2020-06-09 23:40:36 +03:00
2021-07-04 20:24:19 +03:00
// This function's calls will NOT be inlined.
[noinline]
fn function() {
}
// This function will NOT return to its callers.
// Such functions can be used at the end of or blocks,
// just like exit/1 or panic/1. Such functions can not
// have return types, and should end either in for{}, or
// by calling other `[noreturn]` functions.
[noreturn]
fn forever() {
for {}
}
2021-02-28 19:01:31 +03:00
// The following struct must be allocated on the heap. Therefore, it can only be used as a
2021-02-13 17:52:01 +03:00
// reference (`& Window`) or inside another reference (`& OuterStruct{ Window{...} }`).
2021-07-16 21:40:51 +03:00
// See section "Stack and Heap"
2021-02-13 17:52:01 +03:00
[heap]
2020-06-09 23:40:36 +03:00
struct Window {
}
// V will not generate this function and all its calls if the provided flag is false.
// To use a flag, use `v -d flag`
[if debug]
2020-12-06 00:54:41 +03:00
fn foo() {
}
2020-06-09 23:40:36 +03:00
fn bar() {
2020-12-06 00:54:41 +03:00
foo() // will not be called if `-d debug` is not passed
2020-06-09 23:40:36 +03:00
}
2021-04-09 13:13:49 +03:00
// The memory pointed to by the pointer arguments of this function will not be
// freed by the garbage collector (if in use) before the function returns
[keep_args_alive]
fn C.my_external_function(voidptr, int, voidptr) int
2021-03-07 11:03:00 +03:00
// Calls to following function must be in unsafe{} blocks.
// Note that the code in the body of `risky_business()` will still be
// checked, unless you also wrap it in `unsafe {}` blocks.
2021-10-18 21:03:27 +03:00
// This is useful, when you want to have an `[unsafe]` function that
2021-03-07 11:03:00 +03:00
// has checks before/after a certain unsafe operation, that will still
// benefit from V's safety features.
2021-03-01 23:36:37 +03:00
[unsafe]
fn risky_business() {
2021-03-07 11:03:00 +03:00
// code that will be checked, perhaps checking pre conditions
unsafe {
// code that *will not be* checked, like pointer arithmetic,
// accessing union fields, calling other `[unsafe]` fns, etc...
// Usually, it is a good idea to try minimizing code wrapped
// in unsafe{} as much as possible.
// See also [Memory-unsafe code ](#memory-unsafe-code )
}
// code that will be checked, perhaps checking post conditions and/or
// keeping invariants
2021-03-01 23:36:37 +03:00
}
2021-03-07 11:03:00 +03:00
// V's autofree engine will not take care of memory management in this function.
// You will have the responsibility to free memory manually yourself in it.
2021-03-01 23:36:37 +03:00
[manualfree]
fn custom_allocations() {
}
2020-06-09 23:47:41 +03:00
// For C interop only, tells V that the following struct is defined with `typedef struct` in C
2020-07-09 18:14:14 +03:00
[typedef]
2020-12-06 00:54:41 +03:00
struct C.Foo {
}
2020-06-09 23:47:41 +03:00
2020-07-10 14:14:08 +03:00
// Used in Win32 API code when you need to pass callback function
2020-06-09 23:40:36 +03:00
[windows_stdcall]
2020-07-10 14:14:08 +03:00
fn C.DefWindowProc(hwnd int, msg int, lparam int, wparam int)
2021-02-21 12:56:34 +03:00
// Windows only:
// If a default graphics library is imported (ex. gg, ui), then the graphical window takes
// priority and no console window is created, effectively disabling println() statements.
2021-10-31 15:12:04 +03:00
// Use to explicitly create console window. Valid before main() only.
2021-02-21 12:56:34 +03:00
[console]
fn main() {
}
2020-06-09 23:40:36 +03:00
```
2021-02-01 22:09:25 +03:00
## Goto
2021-02-02 20:51:40 +03:00
V allows unconditionally jumping to a label with `goto` . The label name must be contained
within the same function as the `goto` statement. A program may `goto` a label outside
2021-02-28 19:01:31 +03:00
or deeper than the current scope. `goto` allows jumping past variable initialization or
2021-02-15 16:48:24 +03:00
jumping back to code that accesses memory that has already been freed, so it requires
`unsafe` .
2021-02-01 22:09:25 +03:00
```v ignore
2021-02-02 20:51:40 +03:00
if x {
// ...
if y {
2021-02-15 16:48:24 +03:00
unsafe {
goto my_label
}
2021-02-02 20:51:40 +03:00
}
// ...
}
2021-02-01 22:09:25 +03:00
my_label:
```
2021-02-15 16:48:24 +03:00
`goto` should be avoided, particularly when `for` can be used instead.
[Labelled break/continue ](#labelled-break--continue ) can be used to break out of
a nested loop, and those do not risk violating memory-safety.
2020-06-09 23:40:36 +03:00
2020-06-26 20:36:40 +03:00
# Appendices
2019-11-21 04:36:55 +03:00
## Appendix I: Keywords
2020-09-12 14:10:27 +03:00
V has 41 reserved keywords (3 are literals):
2019-11-21 04:36:55 +03:00
2020-11-03 03:04:14 +03:00
```v ignore
2020-06-29 18:59:31 +03:00
as
2020-09-12 14:10:27 +03:00
asm
2020-07-08 09:50:32 +03:00
assert
2020-09-12 14:10:27 +03:00
atomic
2019-11-21 04:36:55 +03:00
break
const
continue
defer
else
2020-09-12 14:10:27 +03:00
embed
2019-11-21 04:36:55 +03:00
enum
2020-07-08 09:50:32 +03:00
false
2019-11-21 04:36:55 +03:00
fn
for
go
goto
if
import
in
interface
2020-06-29 18:59:31 +03:00
is
2020-09-12 14:10:27 +03:00
lock
2019-11-21 04:36:55 +03:00
match
module
2020-07-07 17:57:39 +03:00
mut
2019-11-21 04:36:55 +03:00
none
or
pub
return
2020-09-12 14:10:27 +03:00
rlock
select
2020-09-19 00:48:59 +03:00
shared
2020-09-12 14:10:27 +03:00
sizeof
static
2019-11-21 04:36:55 +03:00
struct
2020-07-08 09:50:32 +03:00
true
2019-11-21 04:36:55 +03:00
type
2020-09-12 14:10:27 +03:00
typeof
union
2020-07-08 09:50:32 +03:00
unsafe
2021-09-22 13:06:30 +03:00
volatile
2021-01-30 14:57:09 +03:00
__offsetof
2019-11-21 04:36:55 +03:00
```
2021-06-22 12:39:57 +03:00
See also [V Types ](#v-types ).
2019-11-21 04:36:55 +03:00
## Appendix II: Operators
2020-07-23 19:55:54 +03:00
This lists operators for [primitive types ](#primitive-types ) only.
2020-11-03 03:04:14 +03:00
```v ignore
2019-11-21 04:36:55 +03:00
+ sum integers, floats, strings
- difference integers, floats
* product integers, floats
/ quotient integers, floats
% remainder integers
2020-10-03 15:07:48 +03:00
~ bitwise NOT integers
2019-11-21 04:36:55 +03:00
& bitwise AND integers
| bitwise OR integers
^ bitwise XOR integers
2020-10-03 15:07:48 +03:00
! logical NOT bools
& & logical AND bools
|| logical OR bools
!= logical XOR bools
2019-11-21 04:36:55 +03:00
< < left shift integer < < unsigned integer
>> right shift integer >> unsigned integer
2021-09-21 14:02:17 +03:00
>>> unsigned right shift integer >> unsigned integer
2019-11-21 04:36:55 +03:00
Precedence Operator
2021-09-21 14:02:17 +03:00
5 * / % < < >> >>> &
2019-11-21 04:36:55 +03:00
4 + - | ^
3 == != < < = > >=
2 & &
1 ||
Assignment Operators
+= -= *= /= %=
& = |= ^=
2021-09-21 14:02:17 +03:00
>>= < < = >>>=
2019-11-21 04:36:55 +03:00
```