Search code examples
oopcrystal-lang

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?


Solution

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