Search code examples
crystal-lang

no overload matches 'Array(Person)#+' with type Person


So I just started learning Crystal because I like both Ruby and C, but I just can't get the hang of the syntax yet. I think I am close, but I'm stuck with this error.
no overload matches 'Array(Person)#+' with type Person Overloads are: - Array(T)#+(other : Array(U)) people += Person.new("Person#{id}")

Here is the code.

class Person
    def initialize(name : String)
        @name = name
        @age = 0
    end

    def name
        @name
    end

    def age
        @age
    end
end

people = [] of Person
counter = 0
id = 0

loop do
    id+=1
    people += Person.new("Person#{id}")

    counter+=1
    break if counter = 5
end

puts(people)

What am I doing wrong? Thanks in advance!


Solution

  • You're trying to put together an Array and a Person. But you can add Array to Array only.

    To solve it, you should use Array#<<, like this: people << Person.new("Person#{id}")

    NOTE: Check your line 25, it should be break if counter == 5