Classes & Objects

Declaring classes, fields, methods, and instances

A class groups fields and methods under a name. Instances are created with new.

Quick reference

class Point
  field x
  function getX()
    return this.x
  end
end

p = new Point()

Declaring a class

A class body holds field declarations and function methods, terminated by end:

class Point
  field x
  function getX()
    return this.x
  end
end
  • field x declares an instance field.
  • A function inside a class is a method; it can reach the instance through this.
  • this.x reads the field x on the current instance.

Creating an instance

Instantiate with new:

p = new Point()

Assign fields with property assignment, and call methods with dotted access:

p.x = 42
print(p.getX())

See also