Variables and constants
let, var and const, how bindings are declared, scoped and reassigned.
PickleScript has three binding forms. The distinction is about mutability, not about type inference, and the type is always written explicitly.
The three forms
const LIMIT: int = 10
fn main() {
const TAX: float = 0.2
let name = "ada" // immutable
var count = 0 // mutable
let ratio: float = 0.5 // annotated
println("{name} {count} {ratio} {LIMIT} {TAX}")
}
| Form | Reassignable | Convention |
|---|---|---|
let |
No | Default choice for values that do not change |
var |
Yes | Only when you truly need to mutate the binding |
const |
No | Named constant, conventionally UPPER_SNAKE_CASE |
const works at module level and inside a function body. It differs from let
only in intent and in naming convention.
The compiler enforces immutability
fn main() {
let name = "ada"
var count = 0
count = 1 // fine
// name = "grace" // error: cannot assign to immutable binding
}
+= and the other compound assignment operators are assignments too, so they
respect the same rule:
fn main() {
var total = 0
total += 5 // fine
let fixed = 1
// fixed += 1 // error: cannot assign to immutable binding
}
Types are explicit
An unannotated let or var is a compile error, not a silently dynamic
binding. Annotate with : T:
fn main() {
let n: int = 3
var s: string = "hi"
var flag: bool = true
}
You may omit the annotation when the initialiser is a literal whose type is
unambiguous, as in let name = "ada".
Scoping
Bindings follow brace scope. A binding is visible from its declaration to the end of its enclosing block, and an inner block may shadow an outer name.
fn main() {
let x = 1
if (true) {
let x = 2
println(x) // 2, shadows the outer x
}
println(x) // 1
}
Reference versus value semantics
Structs, classes and lists use reference semantics: two names bound to the same object observe each other's mutations. Only primitives, strings and enums behave like values.
struct Counter {
var n: int
}
fn main() {
let a = Counter(1)
let b = a
b.n = 99
println(a.n) // 99, a and b name the same object
}
This is why structs and classes do not get structural equality for free:
struct Point {
var x: int
var y: int
}
fn main() {
let a = Point(1, 2)
let b = Point(1, 2)
println(a == b) // false
println(a.x == b.x) // true
}
Compare fields explicitly, or write your own equality function.
Reserved words
Some identifiers are keywords and cannot be used as names. from is one of the
surprising ones, because import { x } from y uses it:
class Range {
// var from: int // error: `from` is reserved
var start: int
}
Common operators
| Operator | Meaning |
|---|---|
+ - * / % |
Arithmetic. / on two ints is integer division. |
== != |
Equality |
< <= > >= |
Ordering, on numbers, char and string |
&& || ! |
Boolean logic. && and || short-circuit. |
& | ^ ~ |
Bitwise. ~6 is -7. |
<< >> |
Shifts |
+= -= *= /= %= and the bitwise/shift forms |
Compound assignment |
fn main() {
println(7 / 2) // 3, integer division
println(7.0 / 2) // 3.5
println(7 % 3) // 1
println(~6) // -7
println(6 & 3) // 2
println(6 | 3) // 7
println(6 ^ 3) // 5
println(1 << 4) // 16
println(32 >> 2) // 8
}
Truthiness
There is none. if and while need a bool; 0, "" and none are not
falsey.
fn main() {
// if (0) { } // type mismatch: expected `bool`, found `int`
if (true) {
println("explicit booleans only")
}
}
Declaration order matters
Bindings must be declared before use in the same scope, exactly like functions:
println(early)
let early = 1
The compiler reports use of undeclared name early and notes that names must be
declared before use.
