mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vfmt: change all '$expr' to '${expr}' (#16428)
This commit is contained in:
116
doc/docs.md
116
doc/docs.md
@@ -416,9 +416,9 @@ In this way, their values can be swapped without an intermediary variable.
|
||||
```v
|
||||
mut a := 0
|
||||
mut b := 1
|
||||
println('$a, $b') // 0, 1
|
||||
println('${a}, ${b}') // 0, 1
|
||||
a, b = b, a
|
||||
println('$a, $b') // 1, 0
|
||||
println('${a}, ${b}') // 1, 0
|
||||
```
|
||||
|
||||
### Declaration errors
|
||||
@@ -605,7 +605,7 @@ converted to a string and embedded into the literal:
|
||||
|
||||
```v
|
||||
name := 'Bob'
|
||||
println('Hello, $name!') // Hello, Bob!
|
||||
println('Hello, ${name}!') // Hello, Bob!
|
||||
```
|
||||
|
||||
It also works with fields: `'age = $user.age'`. If you need more complex expressions, use `${}`:
|
||||
@@ -697,7 +697,7 @@ or use string interpolation (preferred):
|
||||
|
||||
```v
|
||||
age := 12
|
||||
println('age = $age')
|
||||
println('age = ${age}')
|
||||
```
|
||||
|
||||
See all methods of [string](https://modules.vlang.io/index.html#string)
|
||||
@@ -1334,7 +1334,7 @@ m := {
|
||||
'abc': 'def'
|
||||
}
|
||||
if v := m['abc'] {
|
||||
println('the map value for that key is: $v')
|
||||
println('the map value for that key is: ${v}')
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1380,7 +1380,7 @@ import os
|
||||
fn main() {
|
||||
// read text from stdin
|
||||
name := os.input('Enter your name: ')
|
||||
println('Hello, $name!')
|
||||
println('Hello, ${name}!')
|
||||
}
|
||||
```
|
||||
This program can use any public definitions from the `os` module, such
|
||||
@@ -1404,7 +1404,7 @@ import os { input }
|
||||
fn main() {
|
||||
// read text from stdin
|
||||
name := input('Enter your name: ')
|
||||
println('Hello, $name!')
|
||||
println('Hello, ${name}!')
|
||||
}
|
||||
```
|
||||
Note: This will import the module as well. Also, this is not allowed for
|
||||
@@ -1416,7 +1416,7 @@ You can import several specific symbols at once:
|
||||
import os { input, user_os }
|
||||
|
||||
name := input('Enter your name: ')
|
||||
println('Name: $name')
|
||||
println('Name: ${name}')
|
||||
os := user_os()
|
||||
println('Your OS is ${os}.')
|
||||
```
|
||||
@@ -1457,7 +1457,7 @@ fn main() {
|
||||
day: 25
|
||||
}
|
||||
println(time.new_time(my_time).utc_string())
|
||||
println('Century: $my_time.century()')
|
||||
println('Century: ${my_time.century()}')
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1469,11 +1469,11 @@ fn main() {
|
||||
a := 10
|
||||
b := 20
|
||||
if a < b {
|
||||
println('$a < $b')
|
||||
println('${a} < ${b}')
|
||||
} else if a > b {
|
||||
println('$a > $b')
|
||||
println('${a} > ${b}')
|
||||
} else {
|
||||
println('$a == $b')
|
||||
println('${a} == ${b}')
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1739,7 +1739,7 @@ for num in numbers {
|
||||
}
|
||||
names := ['Sam', 'Peter']
|
||||
for i, name in names {
|
||||
println('$i) $name')
|
||||
println('${i}) ${name}')
|
||||
// Output: 0) Sam
|
||||
// 1) Peter
|
||||
}
|
||||
@@ -1807,7 +1807,7 @@ m := {
|
||||
'two': 2
|
||||
}
|
||||
for key, value in m {
|
||||
println('$key -> $value')
|
||||
println('${key} -> ${value}')
|
||||
// Output: one -> 1
|
||||
// two -> 2
|
||||
}
|
||||
@@ -1971,7 +1971,7 @@ fn write_log(s State) !int {
|
||||
// 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')
|
||||
return error('nothing written; file open: ${f.is_opened}')
|
||||
}
|
||||
// the file will be closed here, too
|
||||
return 0
|
||||
@@ -1979,10 +1979,10 @@ fn write_log(s State) !int {
|
||||
|
||||
fn main() {
|
||||
n := write_log(.return_error) or {
|
||||
println('Error: $err')
|
||||
println('Error: ${err}')
|
||||
0
|
||||
}
|
||||
println('$n bytes written')
|
||||
println('${n} bytes written')
|
||||
}
|
||||
```
|
||||
|
||||
@@ -2444,7 +2444,7 @@ clr2 := Rgba32{
|
||||
|
||||
sz := sizeof(Rgba32)
|
||||
unsafe {
|
||||
println('Size: ${sz}B,clr1.b: $clr1.b,clr2.b: $clr2.b')
|
||||
println('Size: ${sz}B,clr1.b: ${clr1.b},clr2.b: ${clr2.b}')
|
||||
}
|
||||
```
|
||||
|
||||
@@ -2859,7 +2859,7 @@ struct Color {
|
||||
}
|
||||
|
||||
pub fn (c Color) str() string {
|
||||
return '{$c.r, $c.g, $c.b}'
|
||||
return '{${c.r}, ${c.g}, ${c.b}}'
|
||||
}
|
||||
|
||||
red := Color{
|
||||
@@ -3024,7 +3024,7 @@ enum Grocery {
|
||||
g1 := int(Grocery.apple)
|
||||
g2 := int(Grocery.orange)
|
||||
g3 := int(Grocery.pear)
|
||||
println('Grocery IDs: $g1, $g2, $g3')
|
||||
println('Grocery IDs: ${g1}, ${g2}, ${g3}')
|
||||
```
|
||||
|
||||
Output: `Grocery IDs: 0, 5, 6`.
|
||||
@@ -3177,7 +3177,7 @@ fn main() {
|
||||
arr << dog
|
||||
arr << cat
|
||||
for item in arr {
|
||||
println('a $item.breed says: $item.speak()')
|
||||
println('a ${item.breed} says: ${item.speak()}')
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -3240,9 +3240,9 @@ interface Something {}
|
||||
|
||||
fn announce(s Something) {
|
||||
if s is Dog {
|
||||
println('a $s.breed dog') // `s` is automatically cast to `Dog` (smart cast)
|
||||
println('a ${s.breed} dog') // `s` is automatically cast to `Dog` (smart cast)
|
||||
} else if s is Cat {
|
||||
println('a cat speaks $s.speak()')
|
||||
println('a cat speaks ${s.speak()}')
|
||||
} else {
|
||||
println('something else')
|
||||
}
|
||||
@@ -3566,7 +3566,7 @@ fn (r Repo) find_user_by_id(id int) !User {
|
||||
return user
|
||||
}
|
||||
}
|
||||
return error('User $id not found')
|
||||
return error('User ${id} not found')
|
||||
}
|
||||
|
||||
// A version of the function using an optional
|
||||
@@ -3709,7 +3709,7 @@ struct PathError {
|
||||
}
|
||||
|
||||
fn (err PathError) msg() string {
|
||||
return 'Failed to open path: $err.path'
|
||||
return 'Failed to open path: ${err.path}'
|
||||
}
|
||||
|
||||
fn try_open(path string) ? {
|
||||
@@ -3750,7 +3750,7 @@ fn new_repo<T>(db DB) Repo<T> {
|
||||
// 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)
|
||||
return r.db.query_one<T>('select * from ${table_name} where id = ?', id)
|
||||
}
|
||||
|
||||
db := new_db()
|
||||
@@ -3856,7 +3856,7 @@ fn main() {
|
||||
g := spawn 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`
|
||||
println('Results: ${h1}, ${h2}') // prints `Results: 16.9, 54.1`
|
||||
}
|
||||
```
|
||||
|
||||
@@ -3867,9 +3867,9 @@ using an array of threads.
|
||||
import time
|
||||
|
||||
fn task(id int, duration int) {
|
||||
println('task $id begin')
|
||||
println('task ${id} begin')
|
||||
time.sleep(duration * time.millisecond)
|
||||
println('task $id end')
|
||||
println('task ${id} end')
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@@ -3906,7 +3906,7 @@ fn main() {
|
||||
}
|
||||
// Join all tasks
|
||||
r := threads.wait()
|
||||
println('All jobs finished: $r')
|
||||
println('All jobs finished: ${r}')
|
||||
}
|
||||
|
||||
// Output: All jobs finished: [1, 4, 9, 16, 25, 36, 49, 64, 81]
|
||||
@@ -4008,16 +4008,16 @@ fn main() {
|
||||
select {
|
||||
a := <-ch {
|
||||
// do something with `a`
|
||||
eprintln('> a: $a')
|
||||
eprintln('> a: ${a}')
|
||||
}
|
||||
b = <-ch2 {
|
||||
// do something with predeclared variable `b`
|
||||
eprintln('> b: $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')
|
||||
eprintln('> c: ${c} was send on channel ch3')
|
||||
}
|
||||
500 * time.millisecond {
|
||||
// do something if no channel has become ready within 0.5s
|
||||
@@ -4141,7 +4141,7 @@ struct User {
|
||||
|
||||
data := '{ "name": "Frodo", "lastName": "Baggins", "age": 25 }'
|
||||
user := json.decode(User, data) or {
|
||||
eprintln('Failed to decode json, error: $err')
|
||||
eprintln('Failed to decode json, error: ${err}')
|
||||
return
|
||||
}
|
||||
println(user.name)
|
||||
@@ -4212,7 +4212,7 @@ strings that interpolate variables, etc.
|
||||
```v
|
||||
fn test_assertion_with_extra_message_failure() {
|
||||
for i in 0 .. 100 {
|
||||
assert i * 2 - 45 < 75 + 10, 'assertion failed for i: $i'
|
||||
assert i * 2 - 45 < 75 + 10, 'assertion failed for i: ${i}'
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -4388,8 +4388,8 @@ fn draw_scene() {
|
||||
// ...
|
||||
name1 := 'abc'
|
||||
name2 := 'def ghi'
|
||||
draw_text('hello $name1', 10, 10)
|
||||
draw_text('hello $name2', 100, 10)
|
||||
draw_text('hello ${name1}', 10, 10)
|
||||
draw_text('hello ${name2}', 100, 10)
|
||||
draw_text(strings.repeat(`X`, 10000), 10, 50)
|
||||
// ...
|
||||
}
|
||||
@@ -4452,7 +4452,7 @@ struct RefStruct {
|
||||
|
||||
fn main() {
|
||||
q, w := f()
|
||||
println('q: $q.r.n, w: $w.n')
|
||||
println('q: ${q.r.n}, w: ${w.n}')
|
||||
}
|
||||
|
||||
fn f() (RefStruct, &MyStruct) {
|
||||
@@ -4469,7 +4469,7 @@ fn f() (RefStruct, &MyStruct) {
|
||||
r: &b
|
||||
}
|
||||
x := a.n + c.n
|
||||
println('x: $x')
|
||||
println('x: ${x}')
|
||||
return e, &c
|
||||
}
|
||||
```
|
||||
@@ -4494,7 +4494,7 @@ fn main() {
|
||||
n: 13
|
||||
}
|
||||
x := q.f(&w) // references of `q` and `w` are passed
|
||||
println('q: $q\nx: $x')
|
||||
println('q: ${q}\nx: ${x}')
|
||||
}
|
||||
|
||||
fn (mut a MyStruct) f(b &MyStruct) int {
|
||||
@@ -4537,7 +4537,7 @@ fn main() {
|
||||
r: &m
|
||||
}
|
||||
r.g()
|
||||
println('r: $r')
|
||||
println('r: ${r}')
|
||||
}
|
||||
|
||||
fn (mut r RefStruct) g() {
|
||||
@@ -4630,7 +4630,7 @@ fn use_stack() {
|
||||
x := 7.5
|
||||
y := 3.25
|
||||
z := x + y
|
||||
println('$x $y $z')
|
||||
println('${x} ${y} ${z}')
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@@ -4640,7 +4640,7 @@ fn main() {
|
||||
}
|
||||
r.g()
|
||||
use_stack() // to erase invalid stack contents
|
||||
println('r: $r')
|
||||
println('r: ${r}')
|
||||
}
|
||||
|
||||
fn (mut r RefStruct) g() {
|
||||
@@ -4711,20 +4711,20 @@ sql db {
|
||||
nr_customers := sql db {
|
||||
select count from Customer
|
||||
}
|
||||
println('number of all customers: $nr_customers')
|
||||
println('number of all customers: ${nr_customers}')
|
||||
// V syntax can be used to build queries
|
||||
uk_customers := sql db {
|
||||
select from Customer where country == 'uk' && nr_orders > 0
|
||||
}
|
||||
println(uk_customers.len)
|
||||
for customer in uk_customers {
|
||||
println('$customer.id - $customer.name')
|
||||
println('${customer.id} - ${customer.name}')
|
||||
}
|
||||
// by adding `limit 1` we tell V that there will be only one object
|
||||
customer := sql db {
|
||||
select from Customer where id == 1 limit 1
|
||||
}
|
||||
println('$customer.id - $customer.name')
|
||||
println('${customer.id} - ${customer.name}')
|
||||
// insert a new customer
|
||||
new_customer := Customer{
|
||||
name: 'Bob'
|
||||
@@ -5198,7 +5198,7 @@ Another example, is if you want to embed the version/name from v.mod *inside* yo
|
||||
```v ignore
|
||||
import v.vmod
|
||||
vm := vmod.decode( @VMOD_FILE ) or { panic(err) }
|
||||
eprintln('$vm.name $vm.version\n $vm.description')
|
||||
eprintln('${vm.name} ${vm.version}\n ${vm.description}')
|
||||
```
|
||||
|
||||
### Compile-time reflection
|
||||
@@ -5215,7 +5215,7 @@ struct User {
|
||||
fn main() {
|
||||
$for field in User.fields {
|
||||
$if field.typ is string {
|
||||
println('$field.name is of type string')
|
||||
println('${field.name} is of type string')
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5243,7 +5243,7 @@ fn main() {
|
||||
}
|
||||
// Usage as expression
|
||||
os := $if windows { 'Windows' } $else { 'UNIX' }
|
||||
println('Using $os')
|
||||
println('Using ${os}')
|
||||
// $else-$if branches
|
||||
$if tinyc {
|
||||
println('tinyc')
|
||||
@@ -5578,7 +5578,7 @@ struct Vec {
|
||||
}
|
||||
|
||||
fn (a Vec) str() string {
|
||||
return '{$a.x, $a.y}'
|
||||
return '{${a.x}, ${a.y}}'
|
||||
}
|
||||
|
||||
fn (a Vec) + (b Vec) Vec {
|
||||
@@ -5724,9 +5724,9 @@ fn main() {
|
||||
}
|
||||
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}')
|
||||
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')
|
||||
println('races won by\n- `main()`: ${races_won_by_main}\n- `change()`: ${races_won_by_change}')
|
||||
}
|
||||
```
|
||||
|
||||
@@ -5932,7 +5932,7 @@ fn main() {
|
||||
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.')
|
||||
println('There are ${nr_users} users in the database.')
|
||||
//
|
||||
error_msg := &char(0)
|
||||
query_all_users := 'select * from users'
|
||||
@@ -6219,9 +6219,9 @@ asm amd64 {
|
||||
; r (a) as a // input
|
||||
r (b) as b
|
||||
}
|
||||
println('a: $a') // 100
|
||||
println('b: $b') // 20
|
||||
println('c: $c') // 120
|
||||
println('a: ${a}') // 100
|
||||
println('b: ${b}') // 20
|
||||
println('c: ${c}') // 120
|
||||
```
|
||||
|
||||
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)
|
||||
@@ -6289,7 +6289,7 @@ An example `deploy.vsh`:
|
||||
|
||||
// print command then execute it
|
||||
fn sh(cmd string) {
|
||||
println('❯ $cmd')
|
||||
println('❯ ${cmd}')
|
||||
print(execute_or_exit(cmd).output)
|
||||
}
|
||||
|
||||
@@ -6314,7 +6314,7 @@ sh('ls')
|
||||
// for file in files {
|
||||
// if file.ends_with('.v') {
|
||||
// mv(file, 'build/') or {
|
||||
// println('err: $err')
|
||||
// println('err: ${err}')
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user