Class

A Class represents a blueprint for creating objects. They describe the data (in the form of properties), and the behavior (in the form of getters and methods) of the objects they create.

Objects created via a Class are called instances.

let Counter be class with start, and step defined with
  count as start,
  step as step,

  increment as method as
    update own's count to own's count plus own's step
    return blank
  , and

  to_string as method as
    return "counter at {own's count}"

let counter be new Counter with 0, 2
do counter's increment
print counter # counter at 2

Note: classes in Sparkle do not support inheritance. Instead, we encourage composition through the use of Traits.

Definition

An empty class can be defined using class as the value. By convention, classes are written in PascalCase.

let SomeClass be class

Constructor parameters are defined after with, and class properties are defined after defined with, these must follow the Oxford comma rule.

let Person be class with name, and age defined with
  name as name, and
  age as age

Constructor parameters become optional when specified with a default value using as.

Using the property keyword on constructor parameters automatically defines a property with the same name and value.

let Person be class with property name as "Person", and property age as 0

Parameters defined with a default value are immediately useable in subsequent parameters.

let Vector be class with property x as 0, and property y as x

Initialization

Instances of a class can be created using the new keyword. Classes may also define initialization logic under initialization as.

let Person be class with name, and age defined with
  name as name,
  age as age, and

  initialization as
    print "Initializing {own's name}"

let alice be new Person with "Alice", and 30

Properties of instances may be accessed just like objects.

let alice be new Person with "Alice", and 30

print alice's name # "Alice"
print alice's age # 30

Static Properties

A class may have static properties that can be accessed directly from the class value itself. They're defined using the static keyword.

let Util be class defined with
  static absolute as function with just n as
    if n is less than 0 do
      return minus n
    otherwise
      return n

let x be minus 4
let y be Util's absolute x

Running out of space?

Classes can be defined in a more vertical format as seen below.

let Car be class with
  name as "Pepper's Car",
  property brand as "Honda",
  property model as "Civic", and
  property year as 2000
defined with just
  description as getter as
    return "{own's name} is {own's color}"

Operator Overloading

Classes can overload arithmetic operators.

let Vector be class with property x as 0, and property y as x defined with
  to_string as method as
    return "Vector with {own's x}, and {own's y}"

  overload plus other as
    return new Vector with own's x plus other's x, and own's y plus other's y
  , and

  overload minus other as
    return new Vector with own's x minus other's x, and own's y minus other's y

let v1 be new Vector with just 5
let v2 be new Vector with 1, and 4

print v1 plus v2 # Vector with 6, and 8

For overloading an operator on the right-side, specify right.

overload right divided by other as
  if own's value is equal to 0 then
    throw "cannot divide by zero"

Constructor

Creates a new Class copying the given class.

Getters

Returns a class's property names as a list of strings.

Returns a class's getter names as a list of strings.

Returns a class's method names as a list of strings.

A getter that returns an Iterator to iterate over the class properties.

print "hello world"