Built-in Modules

ScarScript has built-in math, file, sys, and time modules. Use their functions directly; no import statement is required.

math

FunctionSignatureBehavior
math.sqrt(x)(dec) decSquare root.
math.pow(x, y)(dec, dec) decRaises x to the power y.
math.abs(x)(int) intAbsolute value.
math.round(x)(dec) intRounds to the nearest integer; half values round away from zero.
math.rand(lo, hi)(int, int) intPseudo-random integer in the inclusive range [lo, hi].
val root = math.sqrt(16) val die = math.rand(1, 6)

math.rand is not cryptographically secure. Do not use it for secrets or security-sensitive tokens.

file

FunctionSignatureBehavior
file.write(path, content)(str, str)Creates or overwrites a file.
file.append(path, content)(str, str)Appends content, creating the file if needed.
file.read(path)(str) strReads the entire file.
file.exists(path)(str) boolChecks whether a path exists.
file.delete(path)(str)Deletes a file.
file.write("notes.txt", "first line\n") file.append("notes.txt", "another line\n") if file.exists("notes.txt") { val notes = file.read("notes.txt") print(notes) }

File operations report I/O errors by panicking with an operation-specific message. file.read loads the entire file into memory.

sys

sys.env(name: str) str returns an environment variable, or an empty string if it is unset. Combine it with or to provide a default:

val port = sys.env("PORT") or "8080"

time

FunctionSignatureBehavior
time.now()() strReturns local time in YYYY-MM-DD HH:MM:SS format.
time.sleep(ms)(int)Blocks for the requested number of milliseconds.
print("Started at {time.now()}") time.sleep(250)