V Types

Primitive types

  1. // ignore
  2. bool
  3. string
  4. i8 i16 int i64 i128 (soon)
  5. u8 u16 u32 u64 u128 (soon)
  6. rune // represents a Unicode code point
  7. f32 f64
  8. isize, usize // platform-dependent, the size is how many bytes it takes to reference any location in memory
  9. voidptr // this one is mostly used for C interoperability
  10. any // similar to C's void* and Go's interface{}

Please note that unlike C and Go, int is always a 32 bit integer.

There is an exception to the rule that all operators 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 completely into the data range of the type on the other side. These are the allowed possibilities:

  1. // ignore
  2. i8 i16 int i64
  3. f32 f64
  4. u8 u16 u32 u64
  5. ptr
  6. i8 i16 int i64

An int value for example can be automatically promoted to f64 or i64 but not to u32. (u32 would mean loss of the sign for negative values). Promotion from int to f32, however, is currently done automatically (but can lead to precision loss for large values).

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:

  1. // nofmt
  2. u := u16(12)
  3. v := 13 + u // v is of type `u16` - no promotion
  4. x := f32(45.6)
  5. y := x + 3.14 // x is of type `f32` - no promotion
  6. a := 75 // a is of type `int` - default for int literal
  7. b := 14.7 // b is of type `f64` - default for float literal
  8. c := u + a // c is of type `int` - automatic promotion of `u`'s value
  9. d := b + x // d is of type `f64` - automatic promotion of `x`'s value

Strings

  1. // nofmt
  2. name := 'Bob'
  3. assert name.len == 3 // will print 3
  4. assert name[0] == u8(66) // indexing gives a byte, u8(66) == `B`
  5. assert name[1..3] == 'ob' // slicing gives a string 'ob'
  6. // escape codes
  7. windows_newline := '\r\n' // escape special characters like in C
  8. assert windows_newline.len == 2
  9. // arbitrary bytes can be directly specified using `\x##` notation where `#` is
  10. // a hex digit aardvark_str := '\x61ardvark' assert aardvark_str == 'aardvark'
  11. assert '\xc0'[0] == u8(0xc0)
  12. // or using octal escape `\###` notation where `#` is an octal digit
  13. aardvark_str2 := '\141ardvark'
  14. assert aardvark_str2 == 'aardvark'
  15. // Unicode can be specified directly as `\u####` where # is a hex digit
  16. // and will be converted internally to its UTF-8 representation
  17. star_str := '\u2605' // ★
  18. assert star_str == '★'
  19. assert star_str == '\xe2\x98\x85' // UTF-8 can be specified this way too.

In V, a string is a read-only array of bytes. All Unicode characters are encoded using UTF-8:

  1. s := 'hello 🌎' // emoji takes 4 bytes
  2. assert s.len == 10
  3. arr := s.bytes() // convert `string` to `[]u8`
  4. assert arr.len == 10
  5. s2 := arr.bytestr() // convert `[]byte` to `string`
  6. assert s2 == s

String values are immutable. You cannot mutate elements:

  1. // failcompile
  2. mut s := 'hello 🌎'
  3. s[0] = `H` // not allowed

error: cannot assign to s[i] since V strings are immutable

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 convert the byte to a string, use the .ascii_str() method on the byte:

  1. country := 'Netherlands'
  2. println(country[0]) // Output: 78
  3. println(country[0].ascii_str()) // Output: N

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.

For raw strings, prepend r. Escape handling is not done for raw strings:

  1. s := r'hello\nworld' // the `\n` will be preserved as two characters
  2. println(s) // "hello\nworld"

Strings can be easily converted to integers:

  1. s := '42'
  2. n := s.int() // 42
  3. // all int literals are supported
  4. assert '0xc3'.int() == 195
  5. assert '0o10'.int() == 8
  6. assert '0b1111_0000_1010'.int() == 3850
  7. assert '-0b1111_0000_1010'.int() == -3850

For more advanced string processing and conversions, refer to the vlib/strconv module.

String interpolation

Basic interpolation syntax is pretty simple - use $ before a variable name. The variable will be converted to a string and embedded into the literal:

  1. name := 'Bob'
  2. println('Hello, $name!') // Hello, Bob!

It also works with fields: 'age = $user.age'. If you need more complex expressions, use ${}: 'can register = ${user.age > 13}'.

Format specifiers similar to those in C’s printf() are also supported. f, g, x, o, b, etc. are optional and specify the output format. The compiler takes care of the storage size, so there is no hd or llu.

To use a format specifier, follow this pattern:

${varname:[flags][width][.precision][type]}

  • flags: may be zero or more of the following: - to left-align output within the field, 0 to use 0 as the padding character instead of the default space character. (Note: V does not currently support the use of ' or # as format flags, and V supports but doesn’t need + to right-align since that’s the default.)
  • width: may be an integer value describing the minimum width of total field to output.
  • precision: an integer value preceded by a . will guarantee that many digits after the decimal point, if the input variable is a float. Ignored if variable is an integer.
  • type: f and F specify the input is a float and should be rendered as such, e and E specify the input is a float and should be rendered as an exponent (partially broken), g and G specify the input is a float—the renderer will use floating point notation for small values and exponent notation for large values, d specifies the input is an integer and should be rendered in base-10 digits, x and X require an integer and will render it as hexadecimal digits, o requires an integer and will render it as octal digits, b requires an integer and will render it as binary digits, s requires a string (almost never used).

Note: when a numeric type can render alphabetic characters, such as hex strings or special values like infinity, the lowercase version of the type forces lowercase alphabetics and the uppercase version forces uppercase alphabetics.

Also note: in most cases, it’s best to leave the format type empty. Floats will be rendered by default as g, integers will be rendered by default as d, and s is almost always redundant. There are only three cases where specifying a type is recommended:

  • format strings are parsed at compile time, so specifying a type can help detect errors then
  • format strings default to using lowercase letters for hex digits and the e in exponents. Use a uppercase type to force the use of uppercase hex digits and an uppercase E in exponents.
  • format strings are the most convenient way to get hex, binary or octal strings from an integer.

See Format Placeholder Specification for more information.

  1. x := 123.4567
  2. println('[${x:.2}]') // round to two decimal places => [123.46]
  3. println('[${x:10}]') // right-align with spaces on the left => [ 123.457]
  4. println('[${int(x):-10}]') // left-align with spaces on the right => [123 ]
  5. println('[${int(x):010}]') // pad with zeros on the left => [0000000123]
  6. println('[${int(x):b}]') // output as binary => [1111011]
  7. println('[${int(x):o}]') // output as octal => [173]
  8. println('[${int(x):X}]') // output as uppercase hex => [7B]
  9. println('[${10.0000:.2}]') // remove insignificant 0s at the end => [10]
  10. println('[${10.0000:.2f}]') // do show the 0s at the end, even though they do not change the number => [10.00]

String operators

  1. name := 'Bob'
  2. bobby := name + 'by' // + is used to concatenate strings
  3. println(bobby) // "Bobby"
  4. mut s := 'hello '
  5. s += 'world' // `+=` is used to append to a string
  6. println(s) // "hello world"

All operators in V must have values of the same type on both sides. You cannot concatenate an integer to a string:

  1. // failcompile
  2. age := 10
  3. println('age = ' + age) // not allowed

error: infix expr: cannot use int (right expression) as string

We have to either convert age to a string:

  1. age := 11
  2. println('age = ' + age.str())

or use string interpolation (preferred):

  1. age := 12
  2. println('age = $age')

Runes

A rune represents a single Unicode character and is an alias for u32. To denote them, use ` (backticks) :

  1. rocket := `🚀`

A rune can be converted to a UTF-8 string by using the .str() method.

  1. rocket := `🚀`
  2. assert rocket.str() == '🚀'

A rune can be converted to UTF-8 bytes by using the .bytes() method.

  1. rocket := `🚀`
  2. assert rocket.bytes() == [u8(0xf0), 0x9f, 0x9a, 0x80]

Hex, Unicode, and Octal escape sequences also work in a rune literal:

  1. assert `\x61` == `a`
  2. assert `\141` == `a`
  3. assert `\u0061` == `a`
  4. // multibyte literals work too
  5. assert `\u2605` == `★`
  6. assert `\u2605`.bytes() == [u8(0xe2), 0x98, 0x85]
  7. assert `\xe2\x98\x85`.bytes() == [u8(0xe2), 0x98, 0x85]
  8. assert `\342\230\205`.bytes() == [u8(0xe2), 0x98, 0x85]

Note that rune literals use the same escape syntax as strings, but they can only hold one unicode character. Therefore, if your code does not specify a single Unicode character, you will receive an error at compile time.

Also remember that strings are indexed as bytes, not runes, so beware:

  1. rocket_string := '🚀'
  2. assert rocket_string[0] != `🚀`
  3. assert 'aloha!'[0] == `a`

A string can be converted to runes by the .runes() method.

  1. hello := 'Hello World 👋'
  2. hello_runes := hello.runes() // [`H`, `e`, `l`, `l`, `o`, ` `, `W`, `o`, `r`, `l`, `d`, ` `, `👋`]
  3. assert hello_runes.string() == hello

Numbers

  1. a := 123

This will assign the value of 123 to a. By default a will have the type int.

You can also use hexadecimal, binary or octal notation for integer literals:

  1. a := 0x7B
  2. b := 0b01111011
  3. c := 0o173

All of these will be assigned the same value, 123. They will all have type int, no matter what notation you used.

V also supports writing numbers with _ as separator:

  1. num := 1_000_000 // same as 1000000
  2. three := 0b0_11 // same as 0b11
  3. float_num := 3_122.55 // same as 3122.55
  4. hexa := 0xF_F // same as 255
  5. oct := 0o17_3 // same as 0o173

If you want a different type of integer, you can use casting:

  1. a := i64(123)
  2. b := u8(42)
  3. c := i16(12345)

Assigning floating point numbers works the same way:

  1. f := 1.0
  2. f1 := f64(3.14)
  3. f2 := f32(3.14)

If you do not specify the type explicitly, by default float literals will have the type of f64.

Float literals can also be declared as a power of ten:

  1. f0 := 42e1 // 420
  2. f1 := 123e-2 // 1.23
  3. f2 := 456e+2 // 45600

Arrays

An array is a collection of data elements of the same type. An array literal is a list of expressions surrounded by square brackets. An individual element can be accessed using an index expression. Indexes start from 0:

  1. mut nums := [1, 2, 3]
  2. println(nums) // `[1, 2, 3]`
  3. println(nums[0]) // `1`
  4. println(nums[1]) // `2`
  5. nums[1] = 5
  6. println(nums) // `[1, 5, 3]`

An element can be appended to the end of an array using the push operator <<. It can also append an entire array.

  1. mut nums := [1, 2, 3]
  2. nums << 4
  3. println(nums) // "[1, 2, 3, 4]"
  4. // append array
  5. nums << [5, 6, 7]
  6. println(nums) // "[1, 2, 3, 4, 5, 6, 7]"
  1. mut names := ['John']
  2. names << 'Peter'
  3. names << 'Sam'
  4. // names << 10 <-- This will not compile. `names` is an array of strings.

val in array returns true if the array contains val. See in operator.

  1. names := ['John', 'Peter', 'Sam']
  2. println('Alex' in names) // "false"

Array Fields

There are two fields that control the “size” of an array:

  • 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 field automatically but there are cases where the user may want to do manual optimizations (see below).
  1. mut nums := [1, 2, 3]
  2. println(nums.len) // "3"
  3. println(nums.cap) // "3" or greater
  4. nums = [] // The array is now empty
  5. println(nums.len) // "0"

data is a field (of type voidptr) with the address of the first element. This is for low-level unsafe code.

Note that the fields are read-only and can’t be modified by the user.

Array Initialization

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).

The user can explicitly specify the type for the first element: [u8(16), 32, 64, 128]. V arrays are homogeneous (all elements must have the same type). This means that code like [1, 'a'] will not compile.

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:

  1. mut a := []int{len: 10000, cap: 30000, init: 3}

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):

  1. arr := []int{len: 5, init: -1}
  2. // `arr == [-1, -1, -1, -1, -1]`, arr.cap == 5
  3. // Declare an empty array:
  4. users := []int{}

Setting the capacity improves performance of pushing elements to the array as reallocations can be avoided:

  1. mut numbers := []int{cap: 1000}
  2. println(numbers.len) // 0
  3. // Now appending elements won't reallocate
  4. for i in 0 .. 1000 {
  5. numbers << i
  6. }

Note: The above code uses a range for statement.

You can initialize the array by accessing the it variable which gives the index as shown here:

  1. count := []int{len: 4, init: it}
  2. assert count == [0, 1, 2, 3]
  3. mut square := []int{len: 6, init: it * it}
  4. // square == [0, 1, 4, 9, 16, 25]

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:

This example uses Structs and Sum Types to create an array which can handle different types (e.g. Points, Lines) of data elements.

  1. struct Point {
  2. x int
  3. y int
  4. }
  5. struct Line {
  6. p1 Point
  7. p2 Point
  8. }
  9. type ObjectSumType = Line | Point
  10. mut object_list := []ObjectSumType{}
  11. object_list << Point{1, 1}
  12. object_list << Line{
  13. p1: Point{3, 3}
  14. p2: Point{4, 4}
  15. }
  16. dump(object_list)
  17. /*
  18. object_list: [ObjectSumType(Point{
  19. x: 1
  20. y: 1
  21. }), ObjectSumType(Line{
  22. p1: Point{
  23. x: 3
  24. y: 3
  25. }
  26. p2: Point{
  27. x: 4
  28. y: 4
  29. }
  30. })]
  31. */

Multidimensional Arrays

Arrays can have more than one dimension.

2d array example:

  1. mut a := [][]int{len: 2, init: []int{len: 3}}
  2. a[0][1] = 2
  3. println(a) // [[0, 2, 0], [0, 0, 0]]

3d array example:

  1. mut a := [][][]int{len: 2, init: [][]int{len: 3, init: []int{len: 2}}}
  2. a[0][1][1] = 2
  3. println(a) // [[[0, 0], [0, 2], [0, 0]], [[0, 0], [0, 0], [0, 0]]]

Array methods

All arrays can be easily printed with println(arr) and converted to a string with s := arr.str().

Copying the data from the array is done with .clone():

  1. nums := [1, 2, 3]
  2. nums_copy := nums.clone()

Arrays can be efficiently filtered and mapped with the .filter() and .map() methods:

  1. nums := [1, 2, 3, 4, 5, 6]
  2. even := nums.filter(it % 2 == 0)
  3. println(even) // [2, 4, 6]
  4. // filter can accept anonymous functions
  5. even_fn := nums.filter(fn (x int) bool {
  6. return x % 2 == 0
  7. })
  8. println(even_fn)
  1. words := ['hello', 'world']
  2. upper := words.map(it.to_upper())
  3. println(upper) // ['HELLO', 'WORLD']
  4. // map can also accept anonymous functions
  5. upper_fn := words.map(fn (w string) string {
  6. return w.to_upper()
  7. })
  8. println(upper_fn) // ['HELLO', 'WORLD']

it is a builtin variable which refers to the element currently being processed in filter/map methods.

Additionally, .any() and .all() can be used to conveniently test for elements that satisfy a condition.

  1. nums := [1, 2, 3]
  2. println(nums.any(it == 2)) // true
  3. println(nums.all(it >= 2)) // false

There are further built-in methods for arrays:

  • a.repeat(n) concatenates the array elements n times
  • a.insert(i, val) inserts a new element val at index i and shifts all following elements to the right
  • a.insert(i, [3, 4, 5]) inserts several elements
  • a.prepend(val) inserts a value at the beginning, equivalent to a.insert(0, val)
  • a.prepend(arr) inserts elements of array arr at the beginning
  • a.trim(new_len) truncates the length (if new_length < a.len, otherwise does nothing)
  • a.clear() empties the array without changing cap (equivalent to a.trim(0))
  • a.delete_many(start, size) removes size consecutive elements from index start – triggers reallocation
  • a.delete(index) equivalent to a.delete_many(index, 1)
  • a.delete_last() removes the last element
  • a.first() equivalent to a[0]
  • a.last() equivalent to a[a.len - 1]
  • a.pop() removes the last element and returns it
  • a.reverse() makes a new array with the elements of a in reverse order
  • a.reverse_in_place() reverses the order of elements in a
  • a.join(joiner) concatenates an array of strings into one string using joiner string as a separator

See also vlib/arrays.

Sorting Arrays

Sorting arrays of all kinds is very simple and intuitive. Special variables a and b are used when providing a custom sorting condition.

  1. mut numbers := [1, 3, 2]
  2. numbers.sort() // 1, 2, 3
  3. numbers.sort(a > b) // 3, 2, 1
  1. struct User {
  2. age int
  3. name string
  4. }
  5. mut users := [User{21, 'Bob'}, User{20, 'Zarkon'}, User{25, 'Alice'}]
  6. users.sort(a.age < b.age) // sort by User.age int field
  7. users.sort(a.name > b.name) // reverse sort by User.name string field

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.

  1. struct User {
  2. age int
  3. name string
  4. }
  5. mut users := [User{21, 'Bob'}, User{65, 'Bob'}, User{25, 'Alice'}]
  6. custom_sort_fn := fn (a &User, b &User) int {
  7. // return -1 when a comes before b
  8. // return 0, when both are in same order
  9. // return 1 when b comes before a
  10. if a.name == b.name {
  11. if a.age < b.age {
  12. return 1
  13. }
  14. if a.age > b.age {
  15. return -1
  16. }
  17. return 0
  18. }
  19. if a.name < b.name {
  20. return -1
  21. } else if a.name > b.name {
  22. return 1
  23. }
  24. return 0
  25. }
  26. users.sort_with_compare(custom_sort_fn)

Array Slices

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.

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.

  1. nums := [0, 10, 20, 30, 40]
  2. println(nums[1..4]) // [10, 20, 30]
  3. println(nums[..4]) // [0, 10, 20, 30]
  4. println(nums[1..]) // [10, 20, 30, 40]

In V slices are arrays themselves (they are not distinct types). As a result all array operations may be performed on them. E.g. they can be pushed onto an array of the same type:

  1. array_1 := [3, 5, 4, 7, 6]
  2. mut array_2 := [0, 1]
  3. array_2 << array_1[..3]
  4. println(array_2) // `[0, 1, 3, 5, 4]`

A slice is always created with the smallest possible capacity cap == len (see cap above) 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:

  1. mut a := [0, 1, 2, 3, 4, 5]
  2. mut b := a[2..4]
  3. b[0] = 7 // `b[0]` is referring to `a[2]`
  4. println(a) // `[0, 1, 7, 3, 4, 5]`
  5. b << 9
  6. // `b` has been reallocated and is now independent from `a`
  7. println(a) // `[0, 1, 7, 3, 4, 5]` - no change
  8. 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:

  1. mut a := []int{len: 5, cap: 6, init: 2}
  2. mut b := a[1..4]
  3. a << 3
  4. // no reallocation - fits in `cap`
  5. b[2] = 13 // `a[3]` is modified
  6. a << 4
  7. // a has been reallocated and is now independent from `b` (`cap` was exceeded)
  8. b[1] = 3 // no change in `a`
  9. println(a) // `[2, 2, 2, 13, 2, 3, 4]`
  10. println(b) // `[2, 3, 13]`

You can call .clone() on the slice, if you do want to have an independent copy right away:

  1. mut a := [0, 1, 2, 3, 4, 5]
  2. mut b := a[2..4].clone()
  3. b[0] = 7 // NB: `b[0]` is NOT referring to `a[2]`, as it would have been, without the .clone()
  4. println(a) // [0, 1, 2, 3, 4, 5]
  5. println(b) // [7, 3]
Slices with negative indexes

V supports array and string slices with negative indexes. Negative indexing starts from the end of the array towards the start, for example -3 is equal to array.len - 3. Negative slices have a different syntax from normal slices, i.e. you need to add a gate between the array name and the square bracket: a#[..-3]. The gate specifies that this is a different type of slice and remember that the result is “locked” inside the array. The returned slice is always a valid array, though it may be empty:

  1. a := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  2. println(a#[-3..]) // [7, 8, 9]
  3. println(a#[-20..]) // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  4. println(a#[-20..-8]) // [0, 1]
  5. println(a#[..-3]) // [0, 1, 2, 3, 4, 5, 6]
  6. // empty arrays
  7. println(a#[-20..-10]) // []
  8. println(a#[20..10]) // []
  9. println(a#[20..30]) // []

Array method chaining

You can chain the calls of array methods like .filter() and .map() and use the it built-in variable to achieve a classic map/filter functional paradigm:

  1. // using filter, map and negatives array slices
  2. files := ['pippo.jpg', '01.bmp', '_v.txt', 'img_02.jpg', 'img_01.JPG']
  3. filtered := files.filter(it#[-4..].to_lower() == '.jpg').map(it.to_upper())
  4. // ['PIPPO.JPG', 'IMG_02.JPG', 'IMG_01.JPG']

Fixed size arrays

V also supports arrays with fixed size. Unlike ordinary arrays, their length is constant. You cannot append elements to them, nor shrink them. You can only modify their elements in place.

However, access to the elements of fixed size arrays is more efficient, 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.

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:

  1. mut fnums := [3]int{} // fnums is a fixed size array with 3 elements.
  2. fnums[0] = 1
  3. fnums[1] = 10
  4. fnums[2] = 100
  5. println(fnums) // => [1, 10, 100]
  6. println(typeof(fnums).name) // => [3]int
  7. fnums2 := [1, 10, 100]! // short init syntax that does the same (the syntax will probably change)
  8. anums := fnums[..] // same as `anums := fnums[0..fnums.len]`
  9. println(anums) // => [1, 10, 100]
  10. println(typeof(anums).name) // => []int

Note that slicing will cause the data of the fixed size array to be copied to the newly created ordinary array.

Maps

  1. mut m := map[string]int{} // a map with `string` keys and `int` values
  2. m['one'] = 1
  3. m['two'] = 2
  4. println(m['one']) // "1"
  5. println(m['bad_key']) // "0"
  6. println('bad_key' in m) // Use `in` to detect whether such key exists
  7. println(m.keys()) // ['one', 'two']
  8. m.delete('two')

Maps can have keys of type string, rune, integer, float or voidptr.

The whole map can be initialized using this short syntax:

  1. numbers := {
  2. 'one': 1
  3. 'two': 2
  4. }
  5. println(numbers)

If a key is not found, a zero value is returned by default:

  1. sm := {
  2. 'abc': 'xyz'
  3. }
  4. val := sm['bad_key']
  5. println(val) // ''
  1. intm := {
  2. 1: 1234
  3. 2: 5678
  4. }
  5. s := intm[3]
  6. println(s) // 0

It’s also possible to use an or {} block to handle missing keys:

  1. mm := map[string]int{}
  2. val := mm['bad_key'] or { panic('key not found') }

You can also check, if a key is present, and get its value, if it was present, in one go:

  1. m := {
  2. 'abc': 'def'
  3. }
  4. if v := m['abc'] {
  5. println('the map value for that key is: $v')
  6. }

The same optional check applies to arrays:

  1. arr := [1, 2, 3]
  2. large_index := 999
  3. val := arr[large_index] or { panic('out of bounds') }
  4. println(val)
  5. // you can also do this, if you want to *propagate* the access error:
  6. val2 := arr[333]?
  7. println(val2)