Crystal: multi ivar assignment outside initialize is not unpacked

Created on 24 Apr 2019  路  3Comments  路  Source: crystal-lang/crystal

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)

bug topicsemantic

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.

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Sija picture Sija  路  3Comments

lgphp picture lgphp  路  3Comments

oprypin picture oprypin  路  3Comments

costajob picture costajob  路  3Comments

Papierkorb picture Papierkorb  路  3Comments