mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
docs/readmes: format almost all remaining code blocks (#8590)
This commit is contained in:
56
doc/docs.md
56
doc/docs.md
@ -418,7 +418,7 @@ 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:
|
||||
|
||||
```v ignore
|
||||
```v nofmt
|
||||
u := u16(12)
|
||||
v := 13 + u // v is of type `u16` - no promotion
|
||||
x := f32(45.6)
|
||||
@ -718,7 +718,7 @@ numbers.sort() // 1, 2, 3
|
||||
numbers.sort(a > b) // 3, 2, 1
|
||||
```
|
||||
|
||||
```v nofmt
|
||||
```v
|
||||
struct User {
|
||||
age int
|
||||
name string
|
||||
@ -1487,7 +1487,7 @@ assert button.height == 20
|
||||
|
||||
As you can see, both the struct name and braces can be omitted, instead of:
|
||||
|
||||
```v ignore
|
||||
```v oksyntax nofmt
|
||||
new_button(ButtonConfig{text:'Click me', width:100})
|
||||
```
|
||||
|
||||
@ -1499,20 +1499,20 @@ Struct fields are private and immutable by default (making structs immutable as
|
||||
Their access modifiers can be changed with
|
||||
`pub` and `mut`. In total, there are 5 possible options:
|
||||
|
||||
```v nofmt
|
||||
```v
|
||||
struct Foo {
|
||||
a int // private immutable (default)
|
||||
a int // private immutable (default)
|
||||
mut:
|
||||
b int // private mutable
|
||||
c int // (you can list multiple fields with the same access modifier)
|
||||
b int // private mutable
|
||||
c int // (you can list multiple fields with the same access modifier)
|
||||
pub:
|
||||
d int // public immutable (readonly)
|
||||
d int // public immutable (readonly)
|
||||
pub mut:
|
||||
e int // public, but mutable only in parent module
|
||||
e int // public, but mutable only in parent module
|
||||
__global:
|
||||
f int // public and mutable both inside and outside parent module
|
||||
} // (not recommended to use, that's why the 'global' keyword
|
||||
// starts with __)
|
||||
// (not recommended to use, that's why the 'global' keyword starts with __)
|
||||
f int // public and mutable both inside and outside parent module
|
||||
}
|
||||
```
|
||||
|
||||
For example, here's the `string` type defined in the `builtin` module:
|
||||
@ -1587,15 +1587,15 @@ intended for low-level applications like kernels and drivers.
|
||||
|
||||
It is possible to modify function arguments by using the keyword `mut`:
|
||||
|
||||
```v nofmt
|
||||
```v
|
||||
struct User {
|
||||
name string
|
||||
mut:
|
||||
is_registered bool
|
||||
is_registered bool
|
||||
}
|
||||
|
||||
fn (mut u User) register() {
|
||||
u.is_registered = true
|
||||
u.is_registered = true
|
||||
}
|
||||
|
||||
mut user := User{}
|
||||
@ -1803,7 +1803,7 @@ module, and inside it. That restriction is relaxed only for the `main` module
|
||||
constants too, i.e. just `println(numbers)`, not `println(main.numbers)` .
|
||||
|
||||
vfmt takes care of this rule, so you can type `println(pi)` inside the `math` module,
|
||||
and vffmt will automatically update it to `println(math.pi)`.
|
||||
and vfmt will automatically update it to `println(math.pi)`.
|
||||
|
||||
<!--
|
||||
Many people prefer all caps consts: `TOP_CITIES`. This wouldn't work
|
||||
@ -1812,8 +1812,8 @@ They can represent complex structures, and this is used quite often since there
|
||||
are no globals:
|
||||
-->
|
||||
|
||||
```v ignore
|
||||
println('Top cities: $top_cities.filter(.usa)')
|
||||
```v oksyntax
|
||||
println('Top cities: ${top_cities.filter(.usa)}')
|
||||
```
|
||||
|
||||
## Builtin functions
|
||||
@ -1835,12 +1835,16 @@ fn print_backtrace() // print backtraces on stderr
|
||||
`println` is a simple yet powerful builtin function, that can print anything:
|
||||
strings, numbers, arrays, maps, structs.
|
||||
|
||||
```v nofmt
|
||||
struct User{ name string age int }
|
||||
```v
|
||||
struct User {
|
||||
name string
|
||||
age int
|
||||
}
|
||||
|
||||
println(1) // "1"
|
||||
println('hi') // "hi"
|
||||
println([1,2,3]) // "[1, 2, 3]"
|
||||
println(User{name:'Bob', age:20}) // "User{name:'Bob', age:20}"
|
||||
println([1, 2, 3]) // "[1, 2, 3]"
|
||||
println(User{ name: 'Bob', age: 20 }) // "User{name:'Bob', age:20}"
|
||||
```
|
||||
|
||||
## Custom print of types
|
||||
@ -2157,10 +2161,10 @@ That's why you have to declare a `mut` before the `is` expression:
|
||||
|
||||
```v ignore
|
||||
if mut w is Mars {
|
||||
assert typeof(w).name == 'Mars'
|
||||
if w.dust_storm() {
|
||||
println('bad weather!')
|
||||
}
|
||||
assert typeof(w).name == 'Mars'
|
||||
if w.dust_storm() {
|
||||
println('bad weather!')
|
||||
}
|
||||
}
|
||||
```
|
||||
Otherwise `w` would keep its original type.
|
||||
|
Reference in New Issue
Block a user