parser: change array decompose syntax (#7965)
							parent
							
								
									362c21de06
								
							
						
					
					
						commit
						a8dd13f086
					
				|  | @ -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. | ||||
| 
 | ||||
|  |  | |||
|  | @ -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 | ||||
|  |  | |||
|  | @ -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...) | ||||
|       |             ~~~ | ||||
|     4 |     varargs(...123) | ||||
|       |                ~~~ | ||||
|     5 | } | ||||
|  | @ -1,5 +1,5 @@ | |||
| fn varargs(a ...int) { println(a) } | ||||
| 
 | ||||
| fn main() { | ||||
| 	varargs(123...) | ||||
| 	varargs(...123) | ||||
| } | ||||
|  |  | |||
|  | @ -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) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  |  | |||
|  | @ -4,5 +4,5 @@ fn varargs(a ...int) { | |||
| 
 | ||||
| fn main() { | ||||
| 	a := [1, 2, 3] | ||||
| 	varargs(a...) | ||||
| 	varargs(...a) | ||||
| } | ||||
|  |  | |||
|  | @ -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() | ||||
|  |  | |||
|  | @ -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 { | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue