This works as expected
class Foo
def initialize
@a, @b = {1, 2}
end
end
f = Foo.new
pp!({f.@a, typeof(f.@a)}) # => {1, Int32}
pp!({f.@b, typeof(f.@b)}) # => {2, Int32}
https://play.crystal-lang.org/#/r/6s40
This does not
class Foo
@a, @b = {1, 2}
end
f = Foo.new
pp!({f.@a, typeof(f.@a)}) # => {nil, (Int32 | Nil)}
pp!({f.@b, typeof(f.@b)}) # => {nil, (Int32 | Nil)}
https://play.crystal-lang.org/#/r/6s3y
Found in 0.28.0 (but repro in previous versions also)
Another aspect to consider in this that might end up disallowing multi assign in the class body directly is that is not clear the expected semantic when applying annotations:
class Foo
@[Bar]
@a, @b = {1, 2}
end
Does Bar applies to both ivars or just the first. If so, how one would apply an annotation to the second one?
Perhaps
class Foo
@[Bar1], @[Bar2]
@a, @b = {1, 2}
end
I'd simply disallow annotations for multiple assignments. I don't think there is a real use case and you can always avoid the multiple assign or declare the ivars beforehand separately.
Most helpful comment
I'd simply disallow annotations for multiple assignments. I don't think there is a real use case and you can always avoid the multiple assign or declare the ivars beforehand separately.