> Enough, show me some code!
> Okay, okay. Here’s a simple program:
subscript longer_of(_ a: inout String, _ b: inout String): String {
if b.count() > a.count() { yield &b } else { yield &a }
}
fun emphasize(_ z: inout String, strength: Int = 1) {
z.append(repeat_element("!", count: strength)))
}
public fun main() {
var (x, y) = ("Hi", "World")
emphasize(&longer_of[&x, &y])
print("${x} ${y}") // "Hi World!"
}
> This program declares two character strings, appends an exclamation mark to the longest, and prints them both after the mutation. No pointers or references are used (& in Val does not mean “address of”—it simply marks a mutation), and no unnecessary allocation occurs. The result of longer_of is a projection of the longer argument, so the mutation of z by emphasize occurs directly on the value of y. The value is neither copied, nor moved, and yet it is not being passed by reference to emphasize. The body of emphasize owns z in exactly the same way as it owns strength, which is passed by value: z is an independent value that can only be touched by emphasize.> To better understand, notice that longer_of is not a function; it’s a subscript. A subscript does not return a value, it projects one, granting the caller temporary read and/or write access to it.
Now that's interesting. Subscripts seem to be the novel feature of this lang, they are described here https://tour.val-lang.dev/subscripts