new, initialize and allocate

You create an instance of a class by invoking new on that class:

  1. person = Person.new

Here, person is an instance of Person.

We can’t do much with person, so let’s add some concepts to it. A Person has a name and an age. In the “Everything is an object” section we said that an object has a type and responds to some methods, which is the only way to interact with objects, so we’ll need both name and age methods. We will store this information in instance variables, which are always prefixed with an at (@) character. We also want a Person to come into existence with a name of our choice and an age of zero. We code the “come into existence” part with a special initialize method, which is normally called a constructor:

  1. class Person
  2. def initialize(name : String)
  3. @name = name
  4. @age = 0
  5. end
  6. def name
  7. @name
  8. end
  9. def age
  10. @age
  11. end
  12. end

Now we can create people like this:

  1. john = Person.new "John"
  2. peter = Person.new "Peter"
  3. john.name #=> "John"
  4. john.age #=> 0
  5. peter.name #=> "Peter"

(If you wonder why we needed to specify that name is a String but we didn’t need to do it for age, check the global type inference algorithm)

Note that we create a Person with new but we defined the initialization in an initialize method, not in a new method. Why is this so?

The answer is that when we defined an initialize method Crystal defined a new method for us, like this:

  1. class Person
  2. def self.new(name : String)
  3. instance = Person.allocate
  4. instance.initialize(name)
  5. instance
  6. end
  7. end

First, note the self.new notation. This is a class method that belongs to the class Person, not to particular instances of that class. This is why we can do Person.new.

Second, allocate is a low-level class method that creates an uninitialized object of the given type. It basically allocates the necessary memory for the object, then initialize is invoked on it and finally the instance is returned. You generally never invoke allocate, as it is unsafe, but that’s the reason why new and initialize are related.