1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: change array decompose syntax (#7965)

This commit is contained in:
Daniel Däschle 2021-01-09 22:48:23 +01:00 committed by GitHub
parent 362c21de06
commit a8dd13f086
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 17 additions and 12 deletions

View File

@ -7,6 +7,7 @@
- `byte.str()` has been fixed and works like with all other numbers. `byte.ascii_str()` has been added.
- Smart cast in for loops: `for mut x is string {}`.
- `[noinit]` struct attribute to disallow direct struct initialization with `Foo{}`.
- Array decompose: `[1, 2, 3]...` is now `...[1, 2, 3]`
- Treating `enum` as `int` is removed for strict type checking.
- Support `[manualfree] fn f1(){}` and `[manualfree] module m1`, for functions doing their own memory management.

View File

@ -245,9 +245,9 @@ println(sum(2,3)) // 5
// using array decomposition
a := [2,3,4]
println(sum(a...)) // <-- using postfix ... here. output: 9
println(sum(...a)) // <-- using prefix ... here. output: 9
b := [5, 6, 7]
println(sum(b...)) // output: 18
println(sum(...b)) // output: 18
```
## Symbol visibility

View File

@ -1,6 +1,6 @@
vlib/v/checker/tests/decompose_type_err.vv:4:10: error: decomposition can only be used on arrays
vlib/v/checker/tests/decompose_type_err.vv:4:13: error: decomposition can only be used on arrays
2 |
3 | fn main() {
4 | varargs(123...)
| ~~~
5 | }
4 | varargs(...123)
| ~~~
5 | }

View File

@ -1,5 +1,5 @@
fn varargs(a ...int) { println(a) }
fn main() {
varargs(123...)
varargs(...123)
}

View File

@ -1237,8 +1237,8 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
f.write('}')
}
ast.ArrayDecompose {
f.expr(node.expr)
f.write('...')
f.expr(node.expr)
}
}
}

View File

@ -4,5 +4,5 @@ fn varargs(a ...int) {
fn main() {
a := [1, 2, 3]
varargs(a...)
varargs(...a)
}

View File

@ -127,9 +127,13 @@ pub fn (mut p Parser) call_args() []ast.CallArg {
}
mut comments := p.eat_comments()
arg_start_pos := p.tok.position()
mut e := p.expr(0)
mut array_decompose := false
if p.tok.kind == .ellipsis {
p.next()
array_decompose = true
}
mut e := p.expr(0)
if array_decompose {
e = ast.ArrayDecompose{
expr: e
pos: p.tok.position()

View File

@ -34,7 +34,7 @@ fn test_fn_variadic_generic() {
*/
// forwarding
fn variadic_forward_a(a ...string) string {
return variadic_fn_a(a...)
return variadic_fn_a(...a)
}
fn variadic_fn_a(a ...string) string {
@ -66,7 +66,7 @@ fn test_variadic_only_with_no_vargs() {
fn test_array_decomposition_to_vargs() {
a := ['a', 'b', 'c']
assert variadic_fn_a(a...) == 'abc'
assert variadic_fn_a(...a) == 'abc'
}
struct VaTestStruct {