Objects & Outlines

Throughout this tutorial we have seen terms that use the symbol @. We saw:

dath@stáitse('dearg')
ciorcal@stáitse(200, 200, 100)

After this part of the tutorial, you will understand what is happening when we use @.

Objects

An object is a group of actions and values. For example, the actions dath and ciorcal that we saw before are in the object stáitse.

You use the symbol @ to get a member of an object. We write dath@stáitse to get the action and then we call the action with dath@stáitse('dearg').

Setanta also has an object called mata. The mathematical functions sin, cos, tan etc. are in mata. Similarly mata also has the values pi and e. Use them with pi@mata and e@mata.

sin@mata(2*pi@mata) == 0

stáitse and mata are normal values too, you can put them in variables or use them with functions.

s := stáitse
ciorcal@s(300, 300, 250)

Stáitse variable

Outlines

An outline is a full description of an object. We create an outline to lay out the structure of an object. Then we can create the object.

We use the word creatlach to create an outline. creatlach translates as skeleton/outline.

Look at the following code:

creatlach Cow {
    gníomh speak() {
        scríobh('Moo')
    }
}

We created an outline with the name Cow. Cow has one action, the action speak. speak just prints Moo on the console.

Notice that Cow is not an object, it’s an outline. We can create the object with Cow().

creatlach Cow {
    gníomh speak() {
        scríobh('Moo')
    }
}

cow := Cow()

>-- cow is now an object, with the action "speak"

speak@cow()

Put that into the editor and try it out. Create a few other functions or a completely different class!

Changing objects

Objects have memory too, we can store values in objects with =.

>-- Outline with no actions
creatlach EmptyObject {
}

>-- Create a new object
obj := EmptyObject()

>-- Put 'Sara' in name@obj
name@obj = 'Sara'

scríobh(name@obj) >-- This writes 'Sara'
  • At the start we create a new outline without any actions. We call it EmptyObject.
  • Then we type obj := EmptyObject() to create an object from the EmptyObject outline.
  • We use the symbol @ to put ‘Sara’ in the member name with the line name@obj = 'Sara'.
  • Then when we call scríobh with name@obj, it writes ‘Sara’ out on the console.

To see the full power of outlines, we need to talk about the word seo (meaning “this”).

The word “seo”.

We would like to change an object with the actions that are inside the outline of the object. We use the word seo (meaning “this”) to do this. Look at this for example:

creatlach Person {

    gníomh changeName(name) {
        name@seo = name
    }

    gníomh speak() {
        scríobh('Hi! My name is ' + name@seo)
    }
}

me := Person()
changeName@me('Eoin')

speak@me()

And now look at the console:

Hi! My name is Eoin

We used the word seo inside the action changeName and speak.

We create a new object with Person() and we put it in the variable me. Then we used changeName@me('Eoin') to change the name of the person to ‘Eoin’

When we called speak with speak@me(), it wrote ‘Hi! My name is Eoin’ out on the console.

We can use the same outline again and again to make a lot of objects.

The “nua” action

We will now create an outline for a person. We need to create a lot of actions to store the name, age and address:

creatlach Person {
    gníomh changeName(name) {
        name@seo = name
    }
    gníomh changeAge(age) {
        age@seo = age
    }
    gníomh changeAddress(address) {
        address@seo = address
    }
    gníomh speak() {
        scríobh('Hi, my name is', name@seo)
        scríobh('I am', age@seo, 'years old')
        scríobh('I live in', address@seo)
    }
}

When we want to create an object, we have to write a lot of code:

bart := Person()
changeName@bart('Bart Simpson')
changeAge@bart(10)
changeAddress@bart('Springfield')

To solve this problem, we can use a special action, the “nua” action. “Nua” translates as “new”.

When you put an action in an outline with the name “nua”, that action is ran when an object is created from the outline. For example: We can write the Person outline we wrote earlier with the “nua” function like this:

creatlach Person {
    gníomh nua(name, age, address) {
        name@seo = name
        age@seo = age
        address@seo = address
    }
    gníomh speak() {
        scríobh('Hi, my name is', name@seo)
        scríobh('I am', age@seo, 'years old')
        scríobh('I live in', address@seo)
    }
}

We deleted the actions changeName, changeAge and changeAddress, and instead we wrote one action with the name “nua”.

Now, instead of the long piece of code we wrote earlier to create the bart object. We can write this:

bart := Person('Bart Simpson', 10, 'Springfield')
speak@bart()

That’s a lot less code!

When we create a new object with

Person('Bart Simpson', 10, 'Springfield')

It calls the action “nua” in the outline.

Then when we run speak@bart(), it writes the correct the name, age and address on the console.

Bart

Nuashonraíodh: