Class
A class value defines a new type with its own methods and properties. Instances of a class can
then be created with the new keyword.
let Counter be class with start, and step defined with
count as start,
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
call counter's increment
print counter # counter at 2Definition
An empty class can be defined using class as the value. By convention, classes
are written in PascalCase.
let SomeClass be classConstructor 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 ageConstructor 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 0Parameters defined with a default value are immediately useable in subsequent parameters.
let Vector be class with property x as 0, and property y as xNote: classes in Sparkle do not support inheritance. Read more here.
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 30Properties 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 # 30Static 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 call Util's absolute with just xRunning 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
Sparkle's classes allow overloading of operators using the overload keyword.
let Vector be class with property x as 0, and property y as x defined with
override to_string as method as
return "Vector with {own's x}, and {own's y}"
overload plus with just other as
return new Vector with own's x plus other's x, and own's y plus other's y
, and
overload minus with just 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 8Custom Iterable
Classes can define an iterable under iterable as. It must be a class that
implements start, get, and next methods, and takes exactly
one constructor parameter, which will be the instance being iterated over.
let TupleIterable be class with just tuple defined with
start as 0,
get as function with just index as tuple at index, and
next as method with just index as
if index plus 1 is less than tuple's length do
return index plus 1
return blank
let Tuple be class with property a, and property b defined with
length as getter as 2, and
iterable as TupleIterableThis then allows iterating over instances in for each loops.
let tuple be new Tuple with 1, and 2
for each item in tuple do
print item