Built-in Modules
ScarScript has built-in math, file, sys, and time modules. Use their functions directly; no import statement is required.
math
| Function | Signature | Behavior |
|---|---|---|
math.sqrt(x) | (dec) dec | Square root. |
math.pow(x, y) | (dec, dec) dec | Raises x to the power y. |
math.abs(x) | (int) int | Absolute value. |
math.round(x) | (dec) int | Rounds to the nearest integer; half values round away from zero. |
math.rand(lo, hi) | (int, int) int | Pseudo-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
| Function | Signature | Behavior |
|---|---|---|
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) str | Reads the entire file. |
file.exists(path) | (str) bool | Checks 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
| Function | Signature | Behavior |
|---|---|---|
time.now() | () str | Returns 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)