Contents

Roo

Instance methods, getters and properties

Methods

Let’s carry on extending our Person class from the previous section. People usually like to interact so let’s add a method to people greet each other:

class Person {
    init(name, age) {
        self.name = name
        self.age = age
    }
    
    greet(someone) {
        print("Hello " + someone)
    }
}

var peter = Person("Peter", 17)
peter.greet("Tony") # => "Hello Tony"

Properties

A property holds a value. That value can be any object or function. Properties can be added to instances of classes dynamically:

var hulk = Person("Bruce", 45)
hulk.colour = "Green"
print(hulk.colour) # => "Green"

As we saw above properties can also be added to an instance within a class definition, such as the name and age properties. Note that when in a class definition and you want to refer to the particular instance of that class, use the self keyword.

Getters

Getters are methods without parameters. They allow a class to return a computed value. They are invoked with their name without trailing parentheses. Let’s add to the Person class the ability to print out how many days old they are:

class Person {
    init(name, age) {
        self.name = name
        self.age = age
    }
    
    greet(someone) {
        print("Hello " + someone)
    }
    
    days_old {
        return self.age * 365
    }
}

var peter = Person("Peter", 17)
print(peter.days_old + " days") # => "6205 days"

It’s worth remembering that instance methods, getters and properties can be overriden on a per-instance basis simply by assigning a different value to them:

function boo() {
    return "Boo!"
}

var peter = Person("Peter", 17)
print(peter.days_old) # => 6205

peter.days_old = boo()
print(peter.days_old) # => "Boo!"

An object’s text representation

Every object in Roo has a text representation. For simple data types like Text, Number and Boolean literals this is their value. E.g:

var t = "Hello" # "Hello"
var n = 100 # "100"
var b = True # "True"

Other intrinsic data types have more varied text representations such as:

var r = Request() # "<Request instance>"
var f = File("/Users/garry/Desktop") # "Users/garry/Desktop"

If you create a custom class and you try to either print it with the global print() function or concatenate it with a Text object, by default you’ll get something like:

class Person {
    init(name) {
        self.name = name
    }
}
var p = Person("garry")
print(p + " is cool") # "<Person instance> is cool"

The default text representation of "<Person instance>" is a little unhelpful. Fortunately, Roo allows us to provide a getter in the class definition that Roo will call whenever it needs a text representation of an instance of your custom class. Just define a getter named to_text:

class Person {
    init(name) {
        self.name = name
    }
    
    to_text {
        # Lets return this Person's name capitalised.
        return self.name.capitalise
    }
}
var p = Person("garry")
print(p + " is cool") # "Garry is cool"