It’s finally time to find out what all the @
@
being used a few times so far, in expressions like ciorcal@stáitse
and slánuimh_rand@mata
. To find out what it means, we have to talk about
Objects can be thought of as a
age
address
favourite_colour
Objects can also have
How do we use objects? To do that we have to use the “@
”
When we want to access a member of an object, @
” symbol to do that. The syntax is:
<member name>@<object>
Let’s say we have an object in a tree
height
height@tree
to access it.
When we write ciorcal@stáitse
we are saying “ciorcal
from the stáitse
object.”, then we can ciorcal@stáitse(200, 200, 100)
).
We can ciorcal@stáitse(200, 200, 100)
into ciorcal
ciorcal@stáitse
to get the ciorcal
action, then we can save it in a variable, let’s call that variable gníomh_ciorcal
.
Now we can call that action as before by writing:
Try it out to see that it works!:
We make an object by
For example, we can make an outline for an object that represents a person, then we can create a whole
An outline is
We use the creatlach
keyword to make an outline, “creatlach” is the Irish for “outline” (or “skeleton”). The syntax to make an outline is as follows:
For example, here’s how we would create an outline called Simplí
that has no special behaviour. “Simplí” translates as “
Simplí
. When you Simplí
, it will create an object from the Simplí
outline and return it.
We can then make an object from the “Simplí
” outline by calling Simplí
like so:
>-- New outline with no actions
creatlach Simplí {
}
>-- Create an object from the Simplí outline
oibiacht_simplí := Simplí()
We created a new variable called “oibiacht_simplí
” to store the object. “oibiacht simplí” means “simple object”. This object is exactly as it sounds, simple. It doesn’t have any ball
” (meaning “member”) "Ár mball nua"
(“Our new member”). The syntax is just like
>-- New outline with no actions
creatlach Simplí {
}
>-- Create an object from the Simplí outline
oibiacht_simplí := Simplí()
>-- Add a new member to oibiacht_simplí
ball@oibiacht_simplí = "Ár mball nua"
Now we can use ball@oibiacht_simplí
to
>-- New outline with no actions
creatlach Simplí {
}
>-- Create an object from the Simplí outline
oibiacht_simplí := Simplí()
>-- Add a new member to oibiacht_simplí
ball@oibiacht_simplí = "Ár mball nua"
>-- Access the member and write
scríobh(ball@oibiacht_simplí)
Fill in the following
At this point you might be
The setup is this: We want to
Ciorcal
”déan_ciorcal
”x
, y
, ga
and dath
.
>-- Empty outline definition
creatlach Ciorcal {
}
>-- Action to make a circle object with x, y, radius and colour
gníomh déan_ciorcal(x, y, ga, dath) {
>-- Create blank circle
c := Ciorcal()
>-- Set our parameters
x@c = x
y@c = y
ga@c = ga
dath@c = dath
>-- Return the circle
toradh c
}
Now we can create three actions, one to draw the circle, one to get the tarraing
, achar
and imlíne
. Each of these actions only needs to take one parameter, the circle.
>-- Draw the circle
gníomh tarraing(ciorc) {
>-- Set the colour
dath@stáitse(dath@ciorc)
>-- Draw the circle
ciorcal@stáitse(x@ciorc, y@ciorc, ga@ciorc)
}
>-- Return the area, area = pi * r^2
gníomh achar(ciorc) {
toradh pi@mata * ga@ciorc * ga@ciorc
}
>-- Return the perimeter, perimeter = 2 * pi * r
gníomh imlíne(ciorc) {
toradh 2 * pi@mata * ga@ciorc
}
Now we can use our déan_ciorcal
action to create a circle, and our three other actions to draw it, print the area and print the perimeter.
c := déan_ciorcal(100, 100, 50, "dearg")
tarraing(c)
scríobh("Area is:", achar(c))
scríobh("Perimeter is:", imlíne(c))
Try out the whole program!
This is nice pattern, but it’s
This is where outline actions come in, we can move our actions like “tarraing
”, “imlíne
” and “achar
” into the
To do this we use the keyword “seo
”, which means seo
” keyword has a special
When we create an object from an outline, that object is called an seo
keyword in a outline action, it refers to whatever instance of the outline is being used to call the action.
This is easier to see in practice. Let’s change our “tarraing
” action into an outline action. First let’s copy the action as-is into the outline definition. Our outline definition looks like this now:
creatlach Ciorcal {
>-- Draw the circle
gníomh tarraing(ciorc) {
>-- Set the colour
dath@stáitse(dath@ciorc)
>-- Draw the circle
ciorcal@stáitse(x@ciorc, y@ciorc, ga@ciorc)
}
}
Now we can get rid of the ciorc
argument and we can use the “seo
” keyword instead.
creatlach Ciorcal {
>-- Draw the circle
gníomh tarraing() {
>-- Set the colour
dath@stáitse(dath@seo)
>-- Draw the circle
ciorcal@stáitse(x@seo, y@seo, ga@seo)
}
}
tarraing
with a particular circle object c
, we would write tarraing(c)
. Now that tarraing
is an outline action, we can write tarraing@c()
to call it.
When we do this, because c
is an instance of Ciorcal
, everywhere we used seo
, it will point to c
.
Let’s turn “achar
” and “imlíne
” into outline actions too. We ciorc
” seo
instead.
creatlach Ciorcal {
>-- Draw the circle
gníomh tarraing() {
>-- Set the colour
dath@stáitse(dath@seo)
>-- Draw the circle
ciorcal@stáitse(x@seo, y@seo, ga@seo)
}
>-- Return the area, area = pi * r^2
gníomh achar() {
toradh pi@mata * ga@seo * ga@seo
}
>-- Return the perimeter, perimeter = 2 * pi * r
gníomh imlíne() {
toradh 2 * pi@mata * ga@seo
}
}
Now we can replace our calls to “achar
” and “imlíne
” with this:
c := déan_ciorcal(100, 100, 50, "dearg")
tarraing@c()
scríobh("Area is:", achar@c())
scríobh("Perimeter is:", imlíne@c())
achar
, imlíne
and draw
are all outline actions, so Setanta will make the seo
keyword point to c
, because it is an instance of Ciorcal
.
Try it out!:
Outline actions are useful, but can we get rid of the need for the déan_ciorcal
action?
To make a constructor, you create an outline action with the name “nua”, which means
Let’s see a quick example, we’ll make an outline called Person
, and give it a constructor that takes name
.
Let’s add code into the constructor to name
that’s passed in:
Now when we create a new
creatlach Person {
gníomh nua(name) {
scríobh("Created a person called", name)
}
}
me := Person("Eoin")
Try it out! You’ll see that "Created a person called Eoin"
is written on the console.
Now we can get rid of our déan_ciorcal
action, and use a constructor x
, y
, ga
and dath
. We can use the seo
keyword to
Now we can déan_ciorcal
action, and instead of creating c
by writing:
we can write:
Our final program looks like this:
creatlach Ciorcal {
gníomh nua(x, y, ga, dath) {
x@seo = x
y@seo = y
ga@seo = ga
dath@seo = dath
}
>-- Draw the circle
gníomh tarraing() {
>-- Set the colour
dath@stáitse(dath@seo)
>-- Draw the circle
ciorcal@stáitse(x@seo, y@seo, ga@seo)
}
>-- Return the area, area = pi * r^2
gníomh achar() {
toradh pi@mata * ga@seo * ga@seo
}
>-- Return the perimeter, perimeter = 2 * pi * r
gníomh imlíne() {
toradh 2 * pi@mata * ga@seo
}
}
c := Ciorcal(100, 100, 50, "dearg")
tarraing@c()
scríobh("Area is:", achar@c())
scríobh("Perimeter is:", imlíne@c())
Let’s give it a try!