Shapeis either a
Rectangle. Given an object of type
Shapeyou are always sure it's one of those but you need to use pattern matching to discover which one was it.
Shapevalue, you may always use its
areamethod, even though you may not know what was the real constructor used.
counter.count += 1in other languages. In Luna every object is immutable – once it's created in a given way, it will never change. If you write
foo = Circle 15.0,
foowill always remain a
Circlewith the same radius, no matter how it is used. Any method that may seem to mutate the object, actually returns its changed version. So if you have a list and call its
sortmethod, the original list remains unsorted – the sorted list is returned from the method instead and you need to assign this value to another variable if you want to use the sorted version later on.
classkeyword. It is followed by definitions of constructors and methods. Let's consider this definition of the
Shapeclass to better understand its different parts.
self. They are called using the
.operator and the object before
selfinside method definition.
caseconstruction, that allows to change the behavior based on the constructor of some object.
Circleclass as follows:
Circle 2.0 . area
Shapeclass with two constructors:
Rectangle. Each of the constructors stores information essential for a given kind of shape.