docs/readmes: format almost all remaining code blocks (#8590)
parent
576492af4e
commit
58b3a30b47
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`
|
not lead to type promotions, however they default to `int` and `f64`
|
||||||
respectively, when their type has to be decided:
|
respectively, when their type has to be decided:
|
||||||
|
|
||||||
```v ignore
|
```v nofmt
|
||||||
u := u16(12)
|
u := u16(12)
|
||||||
v := 13 + u // v is of type `u16` - no promotion
|
v := 13 + u // v is of type `u16` - no promotion
|
||||||
x := f32(45.6)
|
x := f32(45.6)
|
||||||
|
@ -718,7 +718,7 @@ numbers.sort() // 1, 2, 3
|
||||||
numbers.sort(a > b) // 3, 2, 1
|
numbers.sort(a > b) // 3, 2, 1
|
||||||
```
|
```
|
||||||
|
|
||||||
```v nofmt
|
```v
|
||||||
struct User {
|
struct User {
|
||||||
age int
|
age int
|
||||||
name string
|
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:
|
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})
|
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
|
Their access modifiers can be changed with
|
||||||
`pub` and `mut`. In total, there are 5 possible options:
|
`pub` and `mut`. In total, there are 5 possible options:
|
||||||
|
|
||||||
```v nofmt
|
```v
|
||||||
struct Foo {
|
struct Foo {
|
||||||
a int // private immutable (default)
|
a int // private immutable (default)
|
||||||
mut:
|
mut:
|
||||||
b int // private mutable
|
b int // private mutable
|
||||||
c int // (you can list multiple fields with the same access modifier)
|
c int // (you can list multiple fields with the same access modifier)
|
||||||
pub:
|
pub:
|
||||||
d int // public immutable (readonly)
|
d int // public immutable (readonly)
|
||||||
pub mut:
|
pub mut:
|
||||||
e int // public, but mutable only in parent module
|
e int // public, but mutable only in parent module
|
||||||
__global:
|
__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
|
f int // public and mutable both inside and outside parent module
|
||||||
// starts with __)
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
For example, here's the `string` type defined in the `builtin` 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`:
|
It is possible to modify function arguments by using the keyword `mut`:
|
||||||
|
|
||||||
```v nofmt
|
```v
|
||||||
struct User {
|
struct User {
|
||||||
name string
|
name string
|
||||||
mut:
|
mut:
|
||||||
is_registered bool
|
is_registered bool
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut u User) register() {
|
fn (mut u User) register() {
|
||||||
u.is_registered = true
|
u.is_registered = true
|
||||||
}
|
}
|
||||||
|
|
||||||
mut user := User{}
|
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)` .
|
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,
|
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
|
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:
|
are no globals:
|
||||||
-->
|
-->
|
||||||
|
|
||||||
```v ignore
|
```v oksyntax
|
||||||
println('Top cities: $top_cities.filter(.usa)')
|
println('Top cities: ${top_cities.filter(.usa)}')
|
||||||
```
|
```
|
||||||
|
|
||||||
## Builtin functions
|
## 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:
|
`println` is a simple yet powerful builtin function, that can print anything:
|
||||||
strings, numbers, arrays, maps, structs.
|
strings, numbers, arrays, maps, structs.
|
||||||
|
|
||||||
```v nofmt
|
```v
|
||||||
struct User{ name string age int }
|
struct User {
|
||||||
|
name string
|
||||||
|
age int
|
||||||
|
}
|
||||||
|
|
||||||
println(1) // "1"
|
println(1) // "1"
|
||||||
println('hi') // "hi"
|
println('hi') // "hi"
|
||||||
println([1,2,3]) // "[1, 2, 3]"
|
println([1, 2, 3]) // "[1, 2, 3]"
|
||||||
println(User{name:'Bob', age:20}) // "User{name:'Bob', age:20}"
|
println(User{ name: 'Bob', age: 20 }) // "User{name:'Bob', age:20}"
|
||||||
```
|
```
|
||||||
|
|
||||||
## Custom print of types
|
## Custom print of types
|
||||||
|
@ -2157,10 +2161,10 @@ That's why you have to declare a `mut` before the `is` expression:
|
||||||
|
|
||||||
```v ignore
|
```v ignore
|
||||||
if mut w is Mars {
|
if mut w is Mars {
|
||||||
assert typeof(w).name == 'Mars'
|
assert typeof(w).name == 'Mars'
|
||||||
if w.dust_storm() {
|
if w.dust_storm() {
|
||||||
println('bad weather!')
|
println('bad weather!')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
Otherwise `w` would keep its original type.
|
Otherwise `w` would keep its original type.
|
||||||
|
|
|
@ -4,40 +4,40 @@ A V module for designing terminal UI apps
|
||||||
|
|
||||||
#### Quickstart
|
#### Quickstart
|
||||||
|
|
||||||
```v nofmt
|
```v
|
||||||
import term.ui as tui
|
import term.ui as tui
|
||||||
|
|
||||||
struct App {
|
struct App {
|
||||||
mut:
|
mut:
|
||||||
tui &tui.Context = 0
|
tui &tui.Context = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn event(e &tui.Event, x voidptr) {
|
fn event(e &tui.Event, x voidptr) {
|
||||||
mut app := &App(x)
|
mut app := &App(x)
|
||||||
println(e)
|
println(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn frame(x voidptr) {
|
fn frame(x voidptr) {
|
||||||
mut app := &App(x)
|
mut app := &App(x)
|
||||||
|
|
||||||
app.tui.clear()
|
app.tui.clear()
|
||||||
app.tui.set_bg_color(r: 63, g: 81, b: 181)
|
app.tui.set_bg_color(r: 63, g: 81, b: 181)
|
||||||
app.tui.draw_rect(20, 6, 41, 10)
|
app.tui.draw_rect(20, 6, 41, 10)
|
||||||
app.tui.draw_text(24, 8, 'Hello from V!')
|
app.tui.draw_text(24, 8, 'Hello from V!')
|
||||||
app.tui.set_cursor_position(0, 0)
|
app.tui.set_cursor_position(0, 0)
|
||||||
|
|
||||||
app.tui.reset()
|
app.tui.reset()
|
||||||
app.tui.flush()
|
app.tui.flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
mut app := &App{}
|
mut app := &App{}
|
||||||
app.tui = tui.init(
|
app.tui = tui.init(
|
||||||
user_data: app,
|
user_data: app
|
||||||
event_fn: event,
|
event_fn: event
|
||||||
frame_fn: frame
|
frame_fn: frame
|
||||||
hide_cursor: true
|
hide_cursor: true
|
||||||
)
|
)
|
||||||
app.tui.run()
|
app.tui.run() ?
|
||||||
```
|
```
|
||||||
|
|
||||||
See the `/examples/term.ui/` folder for more usage examples.
|
See the `/examples/term.ui/` folder for more usage examples.
|
||||||
|
|
|
@ -4,51 +4,51 @@
|
||||||
`x.json2` is an experimental JSON parser written from scratch on V.
|
`x.json2` is an experimental JSON parser written from scratch on V.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
```v oksyntax nofmt
|
```v oksyntax
|
||||||
import x.json2
|
import x.json2
|
||||||
import net.http
|
import net.http
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Decoding
|
// Decoding
|
||||||
resp := http.get('https://example.com')?
|
resp := http.get('https://example.com') ?
|
||||||
|
|
||||||
// raw decode
|
// raw decode
|
||||||
raw_person := json2.raw_decode(resp.text)?
|
raw_person := json2.raw_decode(resp.text) ?
|
||||||
|
|
||||||
// Casting `Any` type / Navigating
|
// Casting `Any` type / Navigating
|
||||||
person := raw_person.as_map()
|
person := raw_person.as_map()
|
||||||
name := person['name'].str() // Bob
|
name := person['name'].str() // Bob
|
||||||
age := person['age'].int() // 19
|
age := person['age'].int() // 19
|
||||||
pi := person['pi'].f64() // 3.14....
|
pi := person['pi'].f64() // 3.14....
|
||||||
|
|
||||||
// Constructing an `Any` type
|
// Constructing an `Any` type
|
||||||
mut me := map[string]json2.Any
|
mut me := map[string]json2.Any{}
|
||||||
me['name'] = 'Bob'
|
me['name'] = 'Bob'
|
||||||
me['age'] = 18
|
me['age'] = 18
|
||||||
|
|
||||||
mut arr := []json2.Any
|
mut arr := []json2.Any{}
|
||||||
arr << 'rock'
|
arr << 'rock'
|
||||||
arr << 'papers'
|
arr << 'papers'
|
||||||
arr << json2.null
|
arr << json2.null
|
||||||
arr << 12
|
arr << 12
|
||||||
|
|
||||||
me['interests'] = arr
|
me['interests'] = arr
|
||||||
|
|
||||||
mut pets := map[string]json2.Any
|
mut pets := map[string]json2.Any{}
|
||||||
pets['Sam'] = 'Maltese Shitzu'
|
pets['Sam'] = 'Maltese Shitzu'
|
||||||
me['pets'] = pets
|
me['pets'] = pets
|
||||||
|
|
||||||
// Stringify to JSON
|
// Stringify to JSON
|
||||||
println(me.str())
|
println(me.str())
|
||||||
//{
|
//{
|
||||||
// "name":"Bob",
|
// "name":"Bob",
|
||||||
// "age":18,
|
// "age":18,
|
||||||
// "interests":["rock","papers","scissors",null,12],
|
// "interests":["rock","papers","scissors",null,12],
|
||||||
// "pets":{"Sam":"Maltese"}
|
// "pets":{"Sam":"Maltese"}
|
||||||
//}
|
//}
|
||||||
|
|
||||||
// Encode a struct/type to JSON
|
// Encode a struct/type to JSON
|
||||||
encoded_json := json2.encode<Person>(person2)
|
encoded_json := json2.encode<Person>(person2)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
## Using `decode<T>` and `encode<T>`
|
## Using `decode<T>` and `encode<T>`
|
||||||
|
|
Loading…
Reference in New Issue