Language Reference¶
Files and blocks¶
Source files use
.aja.Blocks are indentation-based and start after
:.
Variables and typing¶
First assignment locks a variable type.
Reassignment must keep the same type.
Immutable bindings use
imut.
Example:
imut version = "v1"
age = 21
age = 22
Containers and comprehensions¶
List and map literals:
nums = [1, 2, 3]
scores = {"a": 10, "b": 20}
Map helpers:
value = scores.get("a")
value = scores.get("missing", 0)
removed = scores.pop("a")
removed = scores.pop("missing", 0)
List comprehension:
squares = [x * x for x in nums]
tail = [x for x in nums if x > 1]
Map comprehension:
m = {str(x): x * x for x in nums if x > 1}
Rules:
Comprehension iterable must be
listormap.Comprehension
ifmust evaluate tobool.Map comprehension keys must evaluate to
str.
Membership operators¶
Supported operators: in and not in.
2 in [1, 2, 3]
"a" in {"a": 1}
"ell" in "hello"
5 not in [1, 2, 3]
Rules:
Right operand must be
list,map, orstring.For
map, membership checks key existence (left side must bestring).For
string, left side must bestring(substring check).For
list, left type must match the list element type.
Functions¶
Function declaration:
fuc add(a: int, b: int) -> int:
return a + b
Default parameters and named arguments:
fuc greet(name: str, prefix: str = "Hi") -> str:
return prefix + ", " + name
greet("Aja")
greet(name = "Aja", prefix = "Hello")
Keyword-only parameters:
fuc greet(name: str, *, prefix: str, suffix: str = "!") -> str:
return prefix + " " + name + suffix
greet("Aja", prefix = "Hi")
Lambda-lite:
inc = fuc(x: int) -> int: x + 1
Multi-return:
fuc parse(v: str) -> (int, error):
if v == "":
return 0, error("empty")
return int(v), error()
n, err = parse("42")
User types and methods¶
type User:
name: str
age: int
fuc (u: User) greet(prefix: str) -> str:
return prefix + " " + u.name
Interfaces¶
interface Speaker:
speak(prefix: str) -> str
fuc say(s: Speaker) -> str:
return s.speak("Hi")
Imports and exports¶
Import all:
import (
"mod" as m
)
Selective import:
import (
{a b} from "mod"
)
Export list:
export (
a,
b
)