Skip to content

Appendix C. Keywords and built-in functions ​

Keywords are words the language reserves; you cannot use them as variable or function names. Built-in functions and built-in methods need no import and work in any Sprig program. The lists here agree with the compiler's own help; you can check them with sprig help language, sprig help types, sprig help numerics, sprig help strings and sprig help collections.

C.1 Keywords ​

Sprig's 36 keywords:

KeywordPurposeFirst appears
andlogical and, short-circuitingChapter 3
asimport alias, match binding, conform ... as parentViewChapters 12, 16, 18
breakleave a loopChapter 6
caseone branch of a matchChapter 12
catchcatch an errorChapter 14
classdeclare a classChapter 11
conformdeclare that a class satisfies a contractChapter 16
continueskip to the next iteration of a loopChapter 6
elifthe next arm of a condition chainChapter 5
elsethe fallback arm of a condition chainChapter 5
enumdeclare an enumChapter 12
falseBool literal: falseChapter 3
finallya block that always runs when a try endsChapter 14
fnfunction type, lambdaChapter 15
foriterateChapter 6
funcdeclare a function or methodChapter 7
generica generic parameter blockChapter 16
ifcondition statement and condition expressionChapter 5
importimport a module or Java classChapters 4, 18
inthe iterated collection; the membership operatorChapters 4, 6
leta name that binds onceChapter 3
matchmatch an enum or variantChapter 12
notlogical notChapter 3
nullthe null literal, which belongs only to T?Chapter 13
orlogical or, short-circuitingChapter 3
passa statement that does nothingChapter 6
requiresa capability constraint on a generic functionChapter 16
returnreturn from a functionChapter 7
rethrowsthe function throws only what its function arguments throwChapter 15
throwthrow an errorChapter 14
throwsdeclare the errors a function may throwChapter 14
trueBool literal: trueChapter 3
trya block that may throwChapter 14
vara name that can be reassignedChapter 3
variantdeclare a variant (a closed set of types)Chapter 12
whilea while loopChapter 6

Two words that look like keywords are not: export and to (in conform A to B) are "contextual words" with a special meaning only in that position, and they can be used as ordinary names:

spr
# export and to are contextual words, usable as ordinary names.
let export = 1
let to = 2
let main = 4
print(export + to + main)
text
7

Type names (Int, String, List, ...) are not keywords either; they are just names declared ahead of time.

C.2 Built-in functions ​

There are only three:

FunctionWhat it doesNotes
print(value)prints one value and ends the linetakes exactly one argument; print(a, b) is SPR-CALL-ARITY; a null can be printed too
range(stop), range(start, stop), range(start, stop, step)returns a List[Int]stop is not included; step may be negative; written directly after for, it counts without building the list
assert(condition), assert(condition, message)throws an Error when the condition is falseuseful in tests and when hunting bugs; without message the text is assertion failed
spr
# The three built-in functions: print, range, assert.
print("hello")
print(range(3))
print(range(1, 4))
print(range(0, 10, 5))
assert(2 > 1, "two is larger")
print("assert passed")
text
hello
[0, 1, 2]
[1, 2, 3]
[0, 5]
assert passed
  • range(3) is [0, 1, 2] and range(1, 4) is [1, 2, 3]: the stop value is not included.
  • range(0, 10, 5) adds 5 each step and gives [0, 5]; a negative step counts down.
  • assert(2 > 1, ...) passes and the program continues.

C.3 Built-in methods ​

Methods are called with .. Here is the full list; parentheses hold the number of parameters or a note.

Number types

TypeMethods
InttoFloatExact(), toFloatLossy(), toInt32Exact(), toDecimal(), divTrunc(b), compareTo(b), toString()
Int (static)Int.abs(n), Int.min(a, b), Int.max(a, b)
Int32toInt(), toFloat(), toDecimal(), divTrunc(b), compareTo(b), toString()
FloattoIntExact() (must be whole), toIntTrunc(), toFloat32Exact(), toFloat32Lossy(), isNaN(), isInfinite(), isFinite(), approxEqual(other, tolerance), compareTo(b), toString()
Float (static)Float.sqrt(x), Float.floor(x), Float.ceil(x), Float.abs(x)
Float32toFloat(), isNaN(), isInfinite(), isFinite(), compareTo(b), toString()
Decimaldivide(divisor, decimals, roundingMode), toIntExact(), toFloatExact(), toFloatLossy(), toJava(), compareTo(b), toString()
Decimal (static)Decimal.parse(text), Decimal.fromJava(bigDecimal)
BigIntdivTrunc(b), toIntExact(), toFloatExact(), toFloatLossy(), toDecimal(), toJava(), compareTo(b), toString()
BigInt (static)BigInt.parse(text), BigInt.fromInt(n), BigInt.fromJava(bigInteger)
BooltoString()

compareTo returns an Int32: negative, zero or positive, in the same order as <; a.compareTo(b) is exactly Java's Comparator.

String

TypeMethods
Stringlength(), isEmpty(), charAt(i), codeAt(i), substring(start), substring(start, end), indexOf(text), lastIndexOf(text), contains(text), startsWith(text), endsWith(text), compareTo(other), toUpperCase(), toLowerCase(), trim(), split(separator), replace(old, new), repeat(n), toInt(), toIntOrNull(), toFloat(), toString()
String (static)String.join(parts, separator), String.fromCode(codePoint)
  • Positions count Unicode code points, not UTF-16 code units (Chapter 4).
  • The end of substring(start, end) is not included.
  • toInt() fails with an error when the text is not an integer; toIntOrNull() returns Int?.
  • String.join takes the list first and the separator second: String.join(["a", "b"], ",").

Collections

TypeMethods
List[T]size(), isEmpty(), get(i), contains(v), indexOf(v), toMutableList(), toList(), map(f), filter(f), forEach(f), toString()
MutableList[T]all List methods, plus append(v), set(i, v), insert(i, v), removeAt(i), remove(v), clear(), sort()
Map[K, V]size(), isEmpty(), get(k) (returns V?), containsKey(k), keys(), values(), toMutableMap(), toMap(), toString()
MutableMap[K, V]all Map methods, plus set(k, v), remove(k), clear()

List and Map are read-only; to change one, convert it first with toMutableList()/toMutableMap() (Chapters 8 and 9).

A run through a few of each:

spr
# Built-in methods: a few common ones from numbers, strings, lists and maps.
# Numbers
print(Int.abs(-7))
print(Int.min(3, 9))
print(Float.sqrt(9.0))
print(7.toFloatExact())
print(2.5.toIntTrunc())
print(7.divTrunc(3))
# Strings
let text = "  Sprig  "
print(text.trim())
print(text.trim().toUpperCase())
print("a-b-c".split("-"))
print("ab".repeat(2))
print(String.join(["x", "y"], ","))
print("3".toInt() + 1)
print("x".toIntOrNull())
# Lists and maps
let xs: MutableList[Int] = [3, 1, 2]
xs.append(4)
xs.sort()
print(xs)
print(xs.contains(2))
print(xs.indexOf(4))
let ages: Map[String, Int] = {"ada": 36}
print(ages.get("ada"))
print("ada" in ages)
print(ages.keys())
text
7
3
3.0
7.0
2
2
Sprig
SPRIG
[a, b, c]
abab
x,y
4
null
[1, 2, 3, 4]
true
3
36
true
[ada]
  • Int.abs(-7) is 7 and Int.min(3, 9) is 3: static methods are called on the type name.
  • Float.sqrt(9.0) is 3.0; 7.toFloatExact() is 7.0; 2.5.toIntTrunc() is 2; 7.divTrunc(3) is 2.
  • trim() strips the whitespace at both ends and toUpperCase() uppercases; split("-") returns a list; repeat repeats a string; String.join builds x,y.
  • "3".toInt() + 1 is 4; "x".toIntOrNull() prints null.
  • xs.append(4) and then xs.sort() give [1, 2, 3, 4]; contains and indexOf look up by value.
  • ages.get("ada") is 36; "ada" in ages tests keys; keys() returns [ada].

The @std modules of the standard library hold many more functions (list helpers, text, files, JSON, ...). Those need an import and are not "built in" in the sense of this appendix. To see a module's full signatures, run sprig api @std/<module>.spr.

Sprig is licensed under Apache-2.0 · v0.7.1-beta.1 published as a prerelease