mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
go back to mut
. decisions, decisions...
This commit is contained in:
52
doc/docs.md
52
doc/docs.md
@ -126,16 +126,16 @@ Unlike most other languages, V only allows defining variables in functions.
|
||||
Global (module level) variables are not allowed. There's no global state in V.
|
||||
|
||||
```v
|
||||
var age := 20
|
||||
mut age := 20
|
||||
println(age)
|
||||
age = 21
|
||||
println(age)
|
||||
```
|
||||
|
||||
To change the value of the variable use `=`. In V, variables are
|
||||
immutable by default. To be able to change the value of the variable, you have to declare it with `var`.
|
||||
immutable by default. To be able to change the value of the variable, you have to declare it with `mut`.
|
||||
|
||||
Try compiling the program above after removing `var` from the first line.
|
||||
Try compiling the program above after removing `mut` from the first line.
|
||||
|
||||
Please note the difference between `:=` and `=`
|
||||
`:=` is used for declaring and initializing, `=` is used for assigning.
|
||||
@ -200,7 +200,7 @@ bobby := name + 'by' // + is used to concatenate strings
|
||||
println(bobby) // "Bobby"
|
||||
|
||||
println(bobby[1..3]) // "ob"
|
||||
var s := 'hello '
|
||||
mut s := 'hello '
|
||||
s += 'world' // `+=` is used to append to a string
|
||||
println(s) // "hello world"
|
||||
```
|
||||
@ -264,7 +264,7 @@ Modules can be imported using keyword `import`. When using types, functions, and
|
||||
## Arrays
|
||||
|
||||
```v
|
||||
var nums := [1, 2, 3]
|
||||
mut nums := [1, 2, 3]
|
||||
println(nums) // "[1, 2, 3]"
|
||||
println(nums[1]) // "2"
|
||||
|
||||
@ -274,7 +274,7 @@ println(nums) // "[1, 2, 3, 4]"
|
||||
nums << [5, 6, 7]
|
||||
println(nums) // "[1, 2, 3, 4, 5, 6, 7]"
|
||||
|
||||
var names := ['John']
|
||||
mut names := ['John']
|
||||
names << 'Peter'
|
||||
names << 'Sam'
|
||||
// names << 10 <-- This will not compile. `names` is an array of strings.
|
||||
@ -322,7 +322,7 @@ println(upper) // ['HELLO', 'WORLD']
|
||||
## Maps
|
||||
|
||||
```v
|
||||
var m := map[string]int // Only maps with string keys are allowed for now
|
||||
mut m := map[string]int // Only maps with string keys are allowed for now
|
||||
m['one'] = 1
|
||||
m['two'] = 2
|
||||
println(m['one']) // "1"
|
||||
@ -414,7 +414,7 @@ If an index is required, an alternative form `for index, value in` can be used.
|
||||
Note, that the value is read-only. If you need to modify the array while looping, you have to use indexing:
|
||||
|
||||
```v
|
||||
var numbers := [1, 2, 3, 4, 5]
|
||||
mut numbers := [1, 2, 3, 4, 5]
|
||||
for i, num in numbers {
|
||||
println(num)
|
||||
numbers[i] = 0
|
||||
@ -422,8 +422,8 @@ for i, num in numbers {
|
||||
```
|
||||
|
||||
```v
|
||||
var sum := 0
|
||||
var i := 0
|
||||
mut sum := 0
|
||||
mut i := 0
|
||||
for i <= 100 {
|
||||
sum += i
|
||||
i++
|
||||
@ -438,7 +438,7 @@ 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.
|
||||
|
||||
```v
|
||||
var num := 0
|
||||
mut num := 0
|
||||
for {
|
||||
num++
|
||||
if num >= 10 {
|
||||
@ -464,7 +464,7 @@ Finally, there's the traditional C style `for` loop. It's safer than the `while`
|
||||
because with the latter it's easy to forget to update the counter and get
|
||||
stuck in an infinite loop.
|
||||
|
||||
Here `i` doesn't need to be declared with `var` since it's always going to be mutable by definition.
|
||||
Here `i` doesn't need to be declared with `mut` since it's always going to be mutable by definition.
|
||||
|
||||
## Match
|
||||
|
||||
@ -557,7 +557,7 @@ button.widget.set_pos(x,y)
|
||||
|
||||
Struct fields are private and immutable by default (making structs immutable as well).
|
||||
Their access modifiers can be changed with
|
||||
`pub` and `var`. In total, there are 5 possible options:
|
||||
`pub` and `mut`. In total, there are 5 possible options:
|
||||
|
||||
```v
|
||||
struct Foo {
|
||||
@ -633,7 +633,7 @@ This is achieved by lack of global variables and all function arguments being im
|
||||
even when references are passed.
|
||||
|
||||
V is not a pure functional language however.
|
||||
It is possible to modify function arguments by using the same keyword `var`:
|
||||
It is possible to modify function arguments by using the same keyword `mut`:
|
||||
|
||||
```v
|
||||
struct User {
|
||||
@ -641,11 +641,11 @@ var:
|
||||
is_registered bool
|
||||
}
|
||||
|
||||
fn (var u User) register() {
|
||||
fn (mut u User) register() {
|
||||
u.is_registered = true
|
||||
}
|
||||
|
||||
var user := User{}
|
||||
mut user := User{}
|
||||
println(user.is_registered) // "false"
|
||||
user.register()
|
||||
println(user.is_registered) // "true"
|
||||
@ -655,18 +655,18 @@ In this example, the receiver (which is simply the first argument) is marked as
|
||||
so `register()` can change the user object. The same works with non-receiver arguments:
|
||||
|
||||
```v
|
||||
fn multiply_by_2(var arr []int) {
|
||||
fn multiply_by_2(mut arr []int) {
|
||||
for i in 0..arr.len {
|
||||
arr[i] *= 2
|
||||
}
|
||||
}
|
||||
|
||||
var nums := [1, 2, 3]
|
||||
multiply_by_2(var nums)
|
||||
mut nums := [1, 2, 3]
|
||||
multiply_by_2(mut nums)
|
||||
println(nums) // "[2, 4, 6]"
|
||||
```
|
||||
|
||||
Note, that you have to add `var` before `nums` when calling this function. This makes
|
||||
Note, that you have to add `mut` before `nums` when calling this function. This makes
|
||||
it clear that the function being called will modify the value.
|
||||
|
||||
It is preferable to return values instead of modifying arguments.
|
||||
@ -677,7 +677,7 @@ For this reason V doesn't allow to modify primitive args like integers, only
|
||||
complex types like arrays and maps.
|
||||
|
||||
Use `user.register()` or `user = register(user)`
|
||||
instead of `register(var user)`.
|
||||
instead of `register(mut user)`.
|
||||
|
||||
V makes it easy to return a modified version of an object:
|
||||
|
||||
@ -734,7 +734,7 @@ fn (foo &Foo) bar() {
|
||||
```
|
||||
|
||||
`foo` is still immutable and can't be changed. For that,
|
||||
`(var foo Foo)` has to be used.
|
||||
`(mut foo Foo)` has to be used.
|
||||
|
||||
In general, V references are similar to Go pointers and C++ references.
|
||||
For example, a tree structure definition would look like this:
|
||||
@ -917,7 +917,7 @@ enum Color {
|
||||
red green blue
|
||||
}
|
||||
|
||||
var color := Color.red
|
||||
mut color := Color.red
|
||||
// V knows that `color` is a `Color`. No need to use `color = Color.green` here.
|
||||
color = .green
|
||||
println(color) // "1" TODO: print "green"?
|
||||
@ -1385,7 +1385,7 @@ serializers for anything:
|
||||
```v
|
||||
// TODO: not implemented yet
|
||||
fn decode<T>(data string) T {
|
||||
var result := T{}
|
||||
mut result := T{}
|
||||
for field in T.fields {
|
||||
if field.typ == 'string' {
|
||||
result.$field = get_string(data, field.name)
|
||||
@ -1398,7 +1398,7 @@ fn decode<T>(data string) T {
|
||||
|
||||
// generates to:
|
||||
fn decode_User(data string) User {
|
||||
var result := User{}
|
||||
mut result := User{}
|
||||
result.name = get_string(data, 'name')
|
||||
result.age = get_int(data, 'age')
|
||||
return result
|
||||
@ -1492,7 +1492,7 @@ Run `v translate test.cpp` and V will generate `test.v`:
|
||||
|
||||
```v
|
||||
fn main {
|
||||
var s := []
|
||||
mut s := []
|
||||
s << 'V is '
|
||||
s << 'awesome'
|
||||
println(s.len)
|
||||
|
Reference in New Issue
Block a user