January 2019
Intermediate to advanced
246 pages
5h 23m
English
When inspecting the standard library code,[36] you can see that lots of the building blocks in there are actually modules. Let’s take a quick look at some of them.
Suppose you want to be able to order the objects of a type, using operators < and >=, but also ==. Would you define all these comparison operators yourself? Of course not, because the generic module Comparable(T) has already done this for you. By including this module, you get the operators for free. There’s only one catch: Comparable has one abstract method that your class has to implement—the so-called “spaceship operator”: abstract def <=>(other : T).
All other comparisons are defined using <=>. Your code must work like this: compare ...
Read now
Unlock full access