Raku Programming/Classes And Attributes

Classes and Objects
What we've seen so far are the building blocks for procedural programming: Lists of expressions, branches, loops, and subroutines that tell the computer what job to do and exactly how to do it. Raku supports procedural programming very well, but this isn't the only style of programming that Raku supports. Another common paradigm that Raku fits nicely is the object-oriented approach.

Objects are combinations of data and the operations that act on that data. The data of an object are called its attributes, and the operations of an object are called its methods. In this sense, the attributes define the state and the methods define the behavior of the objects.

Classes are templates for creating objects. When referring to an object of a specific class, it's customary to call the object an instance of the class.

Classes
Classes are defined using the  keyword, and are given a name:

Inside that class declaration you can define attributes, methods, or submethods.

Attributes
Attributes are defined with the  keyword, and are specified with a special syntax. For example, let's consider the following class: The class Point2D defines a point in 3D coordinates with three attributes named x-axis, y-axis and z-axis.

In Raku, all attributes are private and one way to express this explicitly is by using the ! twigil. An attribute declared with the ! twigil can only be accessed directly within the class by using !attribute-name. Another important consequence of declaring attributes this way is that objects cannot be populated by using the default new constructor.

If you declare an attribute with the. twigil instead, a read-only accessor method will be automatically generated. You can think of the. twigil as "attribute + accesor". This accesor, which is a method named after its attribute, can be called from outside the class and return the value of its attribute. In order to allow changes to the attributes through the provided accesors, the trait  must be added to the attributes.

The previous class Point3D could be declared as follows: Given that the. twigil declares a ! twigil and an accesor method, atrributes can always be used with the ! twigil even if they're declared using the. twigil.

Methods
Methods are defined just like normal subroutines except for a few key differences:
 * 1) Methods use the   keyword instead of.
 * 2) Methods have the special variable , which refers to the object that the method is being called on. This is known as the invocant.
 * 3) Methods can access the internal traits of the object directly.

When defining the method, you can specify a different name for the invocant, instead of having to use. To do this, you put it at the beginning of the method's signature and separate it from the rest of the signature with a colon:

In this context, the colon is treated like a special type of comma, so you can write it with additional whitespace if that is easier:

Here's an example:

Objects
Objects are data items whose type is a given class. Objects contain any attributes that the class defines, and also has access to any methods in the class. Objects are created with the  keyword.

Using the class Point3D:

The class constructor,  can take named methods used to initialize any of the class attributes:

Methods from that class are called using the dot notation. This is the object, a period, and the name of the method after that.

When an object isn't provided for dot notation method calls, the default variable  is used instead:

Inheritance
Basic class systems enable data and the code routines that operate on that data to be bundled together in a logical way. However, there are more advanced features of most class systems that enable inheritance too, which allows classes to build upon one another. Inheritance is the ability for classes to form logical hierarchies. Raku supports normal inheritance of classes and subclasses, but also supports special advanced features called mixins, and roles. We are going to have to reserve some of the more difficult of these features for later chapters, but we will introduce some of the basics here.

Basic Types
We've talked about a few of Perl's basic types in earlier chapters. It may surprise you to know that all Raku data types are classes, and that all these values have built-in methods that can be used. Here we're going to talk about some of the methods that can be called on some of the various objects that we've seen so far.

and
We've already seen the  and   builtin functions. All built-in classes have methods of the same name that print a stringified form of the object.

and
We're going to take a quick digression and talk about the  function. lets us compile and execute a string of Raku code at runtime.

All Raku objects have a method called  that returns a string of Raku code representing that object.

Context and Coercion methods
There are a number of methods that can be called to explicitly change the given data item into a different form. This is like an explicit way to force the given data item to be taken in a different context. Here is a partial list:


 * : Returns the item in scalar context.
 * : Returns an iterator for the object. We'll talk about iterators in a later chapter.
 * : Returns the object in hash context
 * : Returns the object in array or "list" context
 * : Returns the boolean value of the object
 * : Returns an array containing the object data
 * : Returns a hash containing the object data
 * : Returns an iterator for the object. We'll talk about iterators in a later chapter.
 * : Returns a scalar reference to the object
 * : Returns a string representation for the object

Introspection Methods

 * : Returns a code reference for the object types autovivification closure. We'll talk about autovivification and closures later.
 * : Returns the memory location address of the data object
 * : Returns the objects identity value, which for most objects is just it's memory location (it's .WHERE)
 * : (HOW = Higher Order Workings) Returns the meta class which handles this object
 * : Returns the type object of the current object