Bindings
The object denoted by a mutable binding can be modified, whereas that of an immutable binding cannot.
Immutable bindings are declared with let
and can be initialized with the =
operator. It is not possible to modify the bound object during the lifetime of the binding.
Mutable bindings are typically declared with var
.
Bindings declared with inout
are also mutable but operate differently. They project the value of an object, or part of its value, mutably.
Note however that such a projection is not a reference in the usual sense; it has full ownership over the value it projects, which cannot be accessed except through that projection.
Lifetime
The lifetime of a binding denotes the region of the program where the value of that binding is accessed. The lifetime always ends after the last expression in which the binding occurs. For example, the lifetime of weight
ends after the first call to print
in the program below:
Some operations are said to be consuming, because they force-end the lifetime of a binding. In other words, they must be the last use of the consumed binding. For example, assigning into a var
binding consumes the source of the assignment. Similarly, tuple initialization consumes the source values.
The program above is illegal because the values of weight
and base_length
are used after being consumed to initialize other objects. The Hylo compiler will suggest that you change the code to consume copies of weight
and base_length
instead, and will offer to insert these copies for you. This design follows from two of Hylo's core principles:
Copies are explicit by default. Languages that copy most values implicitly (C++, Swift, R, …) often do so at great expense to performance, and avoiding implicit copies can itself incur a great expense in code size, in code and language complexity, and in development speed. Fortunately, Hylo naturally needs far fewer copies than other languages, so explicit copies in code are always salient rather than “noisy.” (For code where implicit copying is appropriate, Hylo offers a scoped
@implicitcopy
directive).Distinct bindings and distinct objects have independent values. Languages that allow two accessible names to bind to the same mutable object (JavaScript, Python, Ruby, Lua, parts of C++ and Swift) are prone to hidden interactions, race conditions, and easily scale up into systems that can't be documented, tested, or understood. Hylo allows access to a mutable value through exactly one binding at any given time.
Last updated