Sparkle Reference Sheet
Hello World
print "hello world"Comments
# this is a commentPrimitives
let some_blank be blank
let some_boolean be true
let some_integer be 42
let some_float be 3.14
let some_string be "hello world"
update some_string to "the answer is {some_integer}"
let long_message be quote
| this is a very long
| string with many lines
| and even interpolation:
| {some_boolean}- Note: Primitives behave like instances of their respective classes.
Lists
let fruits be list with "apple", "banana", and "orange"
let first_fruit be fruits at 0- For single items, use
with just. Read about the Oxford comma.
let one_item be list with just "me"Objects
let alice be object with
name as "alice",
age as 22, and
greeting as method as
print "hi i'm {own's name}"
do alice's greetingFunctions
let hello be function as
print "hello world"
let greet be function with just name as
print "hello {name}!"
let sum be function with a, and b as
return a plus b
do hello
do greet with just "alice"
do sum with 1, and 2- Blocks are expressions, the last expression is returned as its value.
let sum be function with a, and b as
a plus b # <- no return required- You may omit
dowhen using parameters. - You may also omit
with justwhen using exactly one parameter.
print sum with 1, and 2
greet "alice"Conditionals
let x be 5
if x is greater than 10 then
print "x is big"
otherwise if x is less than 10 then
print "x is small"
otherwise
print "x is just right"- Since blocks are expressions, you can use
if otherwiseinline.
let enabled be true
print if enabled then "it's on!" otherwise "it's off!"Loops
while x is greater than 0 do
print "remaining: {x}"
update x to x minus 1
for each index in 1 until 5 do
print index # 1, 2, 3, 4
for each index in 1 to 5 do
print index # 1, 2, 3, 4, 5
for each index in 0 to 100 by 20 do
print index # 0, 20, 40, 60, 80, 100for eachworks on any Iterable.
for each item in some_list do
print "i like {item}"
for each key in some_object do
print "alice's {key} is {some_object at key}"
for each letter in some_string do
print letterExceptions
let division be function with a, and b as
if b is equal to 0 then
throw "cannot divide by 0"
otherwise
return a divided by b
try
let a be 1
let b be 0
print division with a, and b
catch error
print "{error's name}: {error's message}"
# Error: cannot divide by 0
finally
print "done!"- Since blocks are expressions, you can use
try catchinline. The value of thefinallyblock is ignored.
let c be try
division with a, and b
catch error
print "{error's name}: {error's message}" # blank
finally
print "done!"Classes
let Person be class with name, and age defined with
name as name,
age as age,
greet as method as
print "hello my name is {own's name}"
, and
to_string as method as
name
let alice be new Person with "alice", and 25
do alice's greetTraits
let Vehicle be trait with
name,
wheels, and
drive as method as
print "{own's name} go brr"
let Car be class implementing just Vehicle defined with
name as "car",
wheels as 4, and
to_string as method as
name
let car be new Car
do car's drive # car go brrModules
# relative paths with "./" or "../"
import just lucky_number from "./numbers.sparkle"
print "the lucky number is {lucky_number}!"Keywords
trait, class, object, list, function, method, getter,
by reference, initialization
publicly, privately, static, pass, let, be, update, to,
plus, minus, times, divided by, divided whole by, modulo,
raised to, type of, has, return, if, otherwise, for each,
break, continue, at, while, try, catch, finally, then, do,
with, just, as, in, until, by, and, or, not, exists, extend
is equal to, is not equal to, is less than, is greater than,
is less or equal to, is greater or equal to, implements,
implementing, property, defined with, new, import, from, export
own
quote, raw quote
true, false, blank- Below are variable names imported from the Prelude Module.
print, print_raw, ask, pause, fail, assert, exit,
sleep, throw, integer, float, hex, binary, string
Sparkle, Blank, Boolean, Integer, Float, String, Function,
List, Object, Class, Trait, Range, System, Terminal, Math
FileSystem, Json, Set, Random, BigInteger, Duration,
Instant, Error, Iterable, Iterator, JsonableImportant Notes
- Indentation matters.
- Values are deep copied by default, read References.
- There is no implicit type coercion,
blankis notfalse. - Sparkle uses the Oxford comma for sequences of elements.
- Symbolic operators are also valid (e.g.
+instead ofplus).
print "hello world"