Error: expected block to return Room, not (Int32 | Room)

I feel like I been overusing the kindness of devs here, but.. c:

I get this weird type error when trying to create a two dimensional array of this simple Room type:

class Room
  getter type, name

  def initialize(type : Symbol, name : String)
    @type = type
    @name = name
  end
end

And somewhere else something like this is used to generate a map:

def self.generate_rooms(size)
  Array(Array(Room)).new(size) do |x|
    Array(Room).new(size) do |y|
      # ...
      Room.new :unique, "name"
    end
  end
end  

The full error message:

In room.cr:87:19

 87 | Array(Room).new(size) do |y|
                  ^--
Error: expected block to return Room, not (Int32 | Room)

I’m looking very intensively and can’t see any problem. How is it even possible for the Room constructor to return an union like this?

class Room
  getter type, name

  def initialize(type : Symbol, name : String)
    @type = type
    @name = name
  end
end

def generate_rooms(size)
  Array(Array(Room)).new(size) do |x|
    Array(Room).new(size) do |y|
      # here
      Room.new :unique, "name"
    end
  end
end

p generate_rooms(3)

I can’t reproduce the problem with the above code, so I would guess that the “here” part probably contains code that could return Int32.

1 Like

Ow, now that you mention it, it was just a problem with one of the if conditions returning wrong value inside the “#here” code. Thanks a lot!

1 Like