On this page
article
Chapter 2: Variables and Bindings
Mutable and Immutable
const pi: f64 = 3.14159; -- immutable, must be initialized
var count: i32 = 0; -- mutable
count := count + 1; -- reassignment uses :=
count += 1; -- compound assignment
:= for reassignment, = for initialization and equality comparison. This comes from Pascal. = in expression position is always equality — there is no == in Violet.
if count = 5 { -- = means equality here
debug.print("five!\n");
}
Type Inference
var x = 42; -- inferred as i32
var s = "hello"; -- inferred as []const u8
var f = 3.14; -- inferred as f64
Constants
Constants are compile-time values. They can be used anywhere a comptime value is expected:
const MAX_SIZE: usize = 1024;
const APP_NAME: []const u8 = "myapp";
const VERSION: u32 = 1;
Multiple Bindings
-- destructuring from anonymous struct
var .{ x, y } = getPoint();
-- multiple assignment
var a: i32 = 1;
var b: i32 = 2;
a := b;
b := a; -- this doesn't swap! a is already 2
To swap:
var tmp = a;
a := b;
b := tmp;