Skip to content
This repository has been archived by the owner on Nov 10, 2017. It is now read-only.

Methods and Operators

asterite edited this page Aug 27, 2011 · 1 revision

In Crystal every method and operator belongs to a type. When you write

1 + 2

Crystal translates it to

1.+(2)

which is pretty similar to what Ruby does.

To define the + method for an Int you write

class Int
  def +(other)
    # Whatever... can you self and other
  end
end

By default, Crystal implements this + for Int as a C call that just adds the two ints without overflow check. Take a look at prelude.cr and crystal.c for this.

Clone this wiki locally