Crystal: Playground: Array inference

Created on 30 Aug 2018  路  5Comments  路  Source: crystal-lang/crystal

I was running this code in the playground, and it told me that I found a bug in the playground.

class Test
  property foo = [] of Int32

  def initialize(bar : Int32?)
    @foo = [bar] if bar
    puts @foo
  end
end

test = Test.new(nil)
test = Test.new(1)

The error was this:

Exception

BUG: typeof([bar]) at play:13:15 has no type

I found this while researching a similar issue in a shard I'm writing, where I declared an instance variable with a default value of an empty array of Int32, and the compiler was treating the compile-time type as Array(Int32) | Nil.

bug compiler topicplayground

Most helpful comment

Checked all snippets in this issue. Gives no error in playground for me.


Crystal 0.31.1 on MacOS

$ crystal --version
Crystal 0.31.1 (2019-10-02)

LLVM: 8.0.1
Default target: x86_64-apple-macosx

All 5 comments

What version of Crystal are you using?

0.26.0 on MacOS.

I've got a similar issue on 0.27.0 on MacOS

class Length
  @indicator : IO::Memory?
  @delimiter : IO::Memory?
  def initialize(@token_size : Int32, indicator : String? = nil, delimiter : String? = nil)
      @input = IO::Memory.new
      @indicator = IO::Memory.new(indicator) if indicator
      @delimiter = IO::Memory.new(delimiter) if delimiter
  end
end

With error

BUG: typeof(IO::Memory.new(indicator)) at play:37:15 has no type

The same code in a method, instead of the initializer, works perfectly.
So looks to be related to conditional assignment occurring in an initializer

I simplified the code further to test this and have the same error:

class Length
  @indicator : String?
  def initialize(indicator : String? = nil)
    @indicator = indicator if indicator
  end
end

Checked all snippets in this issue. Gives no error in playground for me.


Crystal 0.31.1 on MacOS

$ crystal --version
Crystal 0.31.1 (2019-10-02)

LLVM: 8.0.1
Default target: x86_64-apple-macosx

Indeed, seems to be working now. Maybe some fix to typeof.

Was this page helpful?
0 / 5 - 0 ratings