the following two ways (but almost same code) that use block for assignment have different effect:
proc what_i_expected() =
let x, y =
block:
var z = 1
inc z
z
assert x == 2 and y == 2
what_i_expected()
let x, y =
block:
var z = 1
inc z
z
assert x == 2
assert y == 3
can anyone explain why y changes?
i also noticed x, y = will evaluate block twice that is quite error-prone for beginner like me.
I'm just a beginner, but I kind of like using let (x, y) = (1, 1) (instead of the shorthand way) because that works everywhere... eg yes I can use the shorthand way if I wanted: var a, b = 1 but I can't then go on to do: a, b = 2 ... but I have to instead do: (a, b) = (2, 2) ... so I always use that format and do even when declaring: var (a, b) = (1, 1) and everthing just works.
That shorthand (when declaring) seems to have a bug or something, but using the shorthand is just another thing for me to remember (and it only works when declaring variables), so I don't tend to use it.
... and the long hand behaves properly:
import print
proc foo =
let (x, y) =
block:
var z = 1
inc z
(z, z)
print x, y # x=2 y=2
foo()
let (x, y) =
block:
var z = 1
inc z
(z, z)
print x, y # x=2 y=2
var cnt = 0
proc count(): (int, int) =
inc cnt
return (cnt, cnt)
let (a, b) = count()
print a, b # a=1 b=1