Classes
Reference types with fields, constructors, methods, inheritance, properties and interfaces.
A class is a reference type. Assigning a class value aliases the same object,
which is what distinguishes it from a primitive.
Basic shape
class Person {
var name: string
var age: int
constructor(name: string, age: int) {
this.name = name
this.age = age
}
fn birthday() -> void {
this.age += 1
}
fn describe() -> string {
return "{this.name} is {this.age}"
}
}
fn main() {
let p = Person("ada", 36)
println(p.describe()) // ada is 36
p.birthday()
println(p.age) // 37
}
- Fields are declared with
varorletand a type. constructoris the primary constructor. Call the type like a function:Person("ada", 36).thisrefers to the current object.- Methods take no
selfparameter.
A let field is assigned once, in the constructor:
class Id {
let value: int
constructor(value: int) {
this.value = value
}
}
Reference semantics
class Person {
var name: string
var age: int
constructor(name: string, age: int) {
this.name = name
this.age = age
}
}
fn main() {
let a = Person("ada", 36)
let b = a
b.age = 99
println(a.age) // 99
}
Named constructors
A method named constructor name becomes a static factory:
class Temp {
var celsius: float
constructor(celsius: float) {
this.celsius = celsius
}
constructor.origin() {
this(0.0) // delegate to the primary constructor
}
fn toFahrenheit() -> float {
return this.celsius * 9.0 / 5.0 + 32.0
}
}
fn main() {
let t = Temp.origin()
println(t.celsius) // 0
println(Temp(100.0).toFahrenheit())
}
Static methods
A static fn belongs to the type rather than an instance, and is called on the
type name:
class Squares {
static fn of(n: int) -> int {
return n * n
}
}
fn main() {
println(Squares.of(7)) // 49
}
Static fields
A static var is one shared binding for the whole type, not one per instance.
Initialise it with = and read or write it through the type name:
class Registry {
static var count: int = 0
static fn bump() -> int {
Registry.count = Registry.count + 1
return Registry.count
}
}
fn main() {
println(Registry.count) // 0
println(Registry.bump()) // 1
println(Registry.bump()) // 2
println(Registry.count) // 2
}
Two instances do not get their own copy, so a static field is the closest thing
PickleScript has to a global. Nothing stops two threads touching it, which is
one more reason to reach for std.thread instead when that is the real
concern.
init blocks
An init block is an instance initialiser. It runs on every construction of
the class, after the constructor's field assignments:
class Registry {
static var count: int = 0
init {
println("init ran")
Registry.count = Registry.count + 1
}
}
fn main() {
println("before") // before
println(Registry.count) // 0
let a = Registry()
let b = Registry()
println(Registry.count) // 2
}
The block is not a class-load hook. Registry.count is still 0 before the
first Registry() is constructed, and each construction runs the body again.
There is a matching deinit block with the same per-instance timing.
Properties
A property is a computed accessor, written as a named block with get:
class Rect {
var width: float
var height: float
constructor(width: float, height: float) {
this.width = width
this.height = height
}
property area: float {
get {
return this.width * this.height
}
}
}
fn main() {
let r = Rect(3.0, 4.0)
println(r.area) // 12
}
Inheritance
A class extends one other class. super calls the parent implementation, and
override marks a replacement.
class Animal {
var name: string
constructor(name: string) {
this.name = name
}
fn speak() -> string {
return "..."
}
fn describe() -> string {
return "{this.name} says {this.speak()}"
}
}
// A subclass with no constructor of its own inherits the base
// constructor. This is the form that runs.
class Dog extends Animal {
override fn speak() -> string {
return "woof"
}
}
fn main() {
let d = Dog("rex")
println(d.describe()) // rex says woof
}
An implicit call to super() happens when you do not write one, provided the
parent has a no-argument constructor.
Interfaces
A class adopts interfaces with implements, and is tested against them with
is:
interface Shape {
fn area() -> float
}
class Square implements Shape {
var side: float
constructor(side: float) {
this.side = side
}
fn area() -> float {
return this.side * this.side
}
}
fn main() {
let s = Square(3.0)
println(s.area()) // 9
println(s is Shape) // true
}
What classes do not support
| Feature | Status |
|---|---|
operator overloads |
Parse, then fail with E0430 |
final |
Not a keyword |
| Visibility modifiers | public / private parse, but are not enforced |
new keyword |
Not used, call the type directly |
| Abstract classes | Not available; use an interface |
| Multiple inheritance | Not available |
Because visibility is not enforced, a "private" field is a naming convention.
Treat anything marked private as internal API.
Classes also do not get structural equality:
class Point {
var x: int
var y: int
constructor(x: int, y: int) {
this.x = x
this.y = y
}
}
fn main() {
let a = Point(1, 2)
let b = Point(1, 2)
println(a == b) // false
}
Compare fields, or write your own equals method.
Rendering
print on a class value prints the type name, and does not call a toString
method even if you define one:
class Money {
var cents: int
constructor(cents: int) {
this.cents = cents
}
fn toString() -> string {
return "Money({this.cents})"
}
}
fn main() {
println(Money(5)) // Money
}
To print a class value rather than its name, interpolate its fields.
