Virtual and abstract types

    If you compile the above program with the tool hierarchy command you will see this for Person:

    1. - class Object
    2. |
    3. +- class Reference
    4. +- class Person
    5. @name : String

    You can see that @pet is Animal+. The + means it’s a virtual type, meaning “any class that inherits from Animal, including Animal“.

    The compiler will always resolve a type union to a virtual type if they are under the same hierarchy:

    The real reason the compiler does this is to be able to compile programs faster by not creating all kinds of different similar unions, also making the generated code smaller in size. But, on the other hand, it makes sense: classes under the same hierarchy should behave in a similar way.

    Lets make John’s pet talk:

      We get an error because the compiler now treats @pet as an , which includes Animal. And since it can’t find a talk method on it, it errors.

      Now the code compiles:

      1. john.pet.talk # => "Woof!"

      Marking a class as abstract will also prevent us from creating an instance of it:

      To make it more explicit that an Animal must define a talk method, we can add it to Animal as an abstract method:

      1. abstract class Animal
      2. # Makes this animal talk

      Abstract methods can also be defined in modules, and the compiler will check that including types implement them.