Shape
is either a Circle
or a Rectangle
. Given an object of type Shape
you are always sure it's one of those but you need to use pattern matching to discover which one was it.Shape
value, you may always use its area
method, even though you may not know what was the real constructor used.counter.count += 1
in 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
, foo
will always remain a Circle
with 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 sort
method, 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.[info] Changes ahead!Currently, there is no way to define classes and methods using the visual editor. All the mechanisms described in these sections are text-only, the support for visual workflow is coming soon.
class
keyword. It is followed by definitions of constructors and methods. Let's consider this definition of the Shape
class to better understand its different parts.self
. They are called using the .
operator and the object before .
becomes the self
inside method definition.case
construction, that allows to change the behavior based on the constructor of some object.Circle
class as follows:Circle 2.0 . area
Shape
class with two constructors: Circle
and Rectangle
. Each of the constructors stores information essential for a given kind of shape.[info] Changes ahead!Currently Luna allows accessing fields by name only when there is exactly one constructor present. This behavior will be extended in the near future, providing great capabilities for multi-constructor classes.