sugarfi
sugarfi

Reputation: 127

Crystal Lang new as a Prefix

I was looking at the source code of the crystal compiler, and I came across this:

    def self.from(obj : Array)
      case obj.size
      when 0
        Nop.new
      when 1
        obj.first
      else
        new obj
      end
    end

In particular, new obj. I know that T.new is used to create a new instance of type T, but I have never seen new x before. Is it some sort of method? I didn't see it defined in that file. What is this new, and what does it do?

Upvotes: 0

Views: 77

Answers (1)

Sergey Fedorov
Sergey Fedorov

Reputation: 4430

Probably it's just Something.new(x). new it's self.new, because you can drop self inside scope.

Upvotes: 1

Related Questions