Variables and scope#
name := value binds a name that cannot be reassigned; var name = value binds one that can.
Save as stock.ms and run ms run stock.ms:
fn main() Void {
count := 3
var remaining = count
remaining -= 1
print(f"{remaining} of {count} left")
}2 of 3 leftGrouping bindings#
var ( declares several mutable bindings, one per line, indented inside the parentheses. Each entry works like a single var binding, including an optional annotation or a destructuring pattern, and later entries see earlier ones.
var (
attempts = 0
limit = attempts + 3
)A group is only a layout: entries are separate bindings with their own scope rules, and the formatter keeps the group together. Separate entries by lines, not commas, and use var [a, b] = pair to destructure. A group cannot sit in an inline body such as if ready { var ( }. Immutable bindings have no group form; write one name := value per line.
Local type inference#
Parameters and return types are explicit; local annotations are optional. count := 3 and count Int := 3 are the same: := marks an immutable binding and the annotation only names its type. var alone binds with =, so var count Int = 3 keeps it. Writing count Int = 3 is rejected with a fix to :=. Constructors also infer types. This continues the Customer and welcome declarations in source files
:
customer := Customer(name: "Ada", active: true)
greeting := welcome(customer)
count Int := 3
var remaining = count
remaining -= 1Destructuring#
Unpack positions or named fields:
[first_name, second_name] := names
{name, email: customer_email} := customer
print(f"{first_name} {second_name} {name} {customer_email}")[...] binds positions; {field: local} renames a field. Field shorthand and nesting work; parenthesized positional bindings do not. See destructuring bindings
.
Scope and shadowing#
- A local name cannot be declared twice in one scope, and a nested scope (a block, match arm, loop, or closure parameter) cannot reuse the name of an enclosing local or parameter:
ms checkrejects the shadowing binding. Rename one of them. A name starting with_is exempt. - A local or match payload that is never read is an error (
local binding x is never read); remove it or start the name with_. Avarthat is only assigned counts as unread, and unfinished code in the editor is not reported. - Every local needs an initial value. The name is visible only after its initializer.
- Locals are visible in source order; module declaration signatures may refer forward.
- A
varkeeps the type from its annotation or initializer; later values must fit it. - Assignment targets a
varbinding or a field reached through mutable storage.:=is immutable including contents; mutation through a parameter or receiver requiresvar. Collection methods that mutate require a mutable receiver and return Void.
fn example() Void {
var remaining = 3
remaining = 2
remaining = 2.0 // Rejected: Float cannot replace Int.
}+=, -=, *=, /=, and %= update a var or field: remaining -= 1 means remaining = remaining - 1. The right side is one operand (total -= a - b subtracts the difference), and += also appends text. See the statement grammar
.
Only a simple name accepts a type annotation. Destructuring infers each local; elisions, rest/default patterns, destructuring assignment, and pattern annotations are rejected. See type compatibility .
Reassignment and references#
A := value cannot be mutated. Primitives copy implicitly. A string, a value of a named type, or a collection has one owner, and binding it works like this:
y := xandy := x.partborrow it.xstays readable and cannot change whileyis used.var y = x.partborrows it exclusively, soy.append(1)changesx.part.var y = xmoves it whenxis not itself avar.
A value moves into a moving position (a field, a collection, or a return of something that is not a parameter) at its last use. Write .clone() for an explicit deep copy when you want an independent value. See mutation and references
.
var n = r.n of a primitive copies the number: n += 1 leaves r.n as it was. A call that returns a part of its argument returns a borrow of it, so var xs = b.list() changes b’s list. var xs = f() owns the result when f builds a new value.
Reassigning an owning var drops the previous value and closes resources it held. A live borrow prevents that reassignment. The replacement closes when its owning scope ends unless it moves to another owner. Details are in the page above.