type annotations for variable tuple unpacking, better error messages (#22611)

* type annotations for variable tuple unpacking, better error messages

closes #17989, closes https://github.com/nim-lang/RFCs/issues/339

* update grammar

* fix test

(cherry picked from commit ba158d73dc)
This commit is contained in:
metagn
2023-09-01 07:26:53 +03:00
committed by narimiran
parent 7c44af4e22
commit 3911c90d7b
6 changed files with 38 additions and 8 deletions

View File

@@ -75,3 +75,20 @@ block: # unary assignment unpacking
var a: int
(a,) = (1,)
doAssert a == 1
block: # type annotations
block: # basic
let (a, b): (int, int) = (1, 2)
doAssert (a, b) == (1, 2)
block: # type inference
let (a, b): (byte, float) = (1, 2)
doAssert (a, b) == (1.byte, 2.0)
block: # type mismatch
doAssert not (compiles do:
let (a, b): (int, string) = (1, 2))
block: # nested
let (a, (b, c)): (int, (int, int)) = (1, (2, 3))
doAssert (a, b, c) == (1, 2, 3)
block: # nested type inference
let (a, (b, c)): (byte, (float, cstring)) = (1, (2, "abc"))
doAssert (a, b, c) == (1.byte, 2.0, cstring"abc")