Classes

Syntax and semantics heavily inspired by Kotlin.

Classes in THP are significantly different than PHP.

Definition

A class is defined as follows:


class Animal() {}
╰╴No statement matched
thp

The name of the class MUST begin with an uppercase letter.

Classes have a parameter list even if they have no parameters for consistency sake.

Instanciation

To create an instance of a class call it as if it was a function, without new.


val animal = Animal()
             ╰╴Invalid variable declaration
thp

Properties

Properties are declared with var/val inside a block. They must explicitly declare their datatype.


class Person() {
╰╴No statement matched
// This is an error. Properties must declare their datatype, // even if the compiler can infer it. val name = "Jane Doe" // This is correct val String name = "Jane Doe" // This is also okay String name = "Jane Doe" }
thp

Properties are private by default, but can be made public with pub.


class Person() {
╰╴No statement matched
// Properties are private by default val String name = "John Doe" // To make a property public use `pub` pub var Int age = 30 } val p = Person() print(p.name) // Compile error: `name` is private print(p.age) // 30
thp

Unlike PHP, to access properties and methods use dot notation . instead of an arrow ->.

Static properties are explained in the Static page.

Readonly properties are explained in the Readonly page.

The interaction between properties and the constructor is explained in the Constructor page.

Methods

Methods are declared with fun, as regular functions.


class Person() {
╰╴No statement matched
fun greet() { print("Hello") } }
thp

Methods are private by default, and are made public with pub.


class Person() {
╰╴No statement matched
// This method is private fun private_greet() { print("Hello from private method") } // Use `pub` to make a method public pub fun greet() { print("Hello from greet") } } val p = Person() p.greet() //: Hello from greet p.private_greet() // Compile time error. Private method.
thp

Unlike PHP, in THP a method cannot have the same name as a property and viceversa. Doing so is a compile error.


class Person() {
╰╴No statement matched
String name = "Rose" // This is a compile error fun name() -> String { "Rose" } }
thp

This

THP uses the dollar sign $ as this inside classes. It is required when using a class property/method.


class Person() {
╰╴No statement matched
val String name = "Jane Doe" pub fun get_name() -> String { return $name } pub fun greet() { val person_name = $get_name() print("Hello, I'm {person_name}") } }
thp

Mutable methods

By default methods cannot mutate the state of the object.


class Animal(var String name) {
╰╴No statement matched
pub fun set_name(String new_name) { $name = new_name // Error: Cannot mutate $name } }
thp

To do so the method must be annotated. The caller must also declare a mutable variable.


class Animal(var String name) {
╰╴No statement matched
pub mut fun set_name(String new_name) { $name = new_name // Ok } } var michi = Animal("Michifu") michi.set_name("Garfield")
thp