def declares
one or more new constants in the current block of code whose type may be inferred from the type of the value expression used to initialize it. The new constant is implicitly declared to be constant and may not be set to a different value after it has been initialized, but if the constant is a class instance its internal state may change.This changes the face of curl coding as can be seen in the following snippet from a server HTTP procedure which you can find in the Curl extended examples. The code is found within a {for } loop expression:
def (num-bytes-decoded, chars) =
{decode-characters
request.underlying-FastArray,
CharEncoding.ascii,
in-end = i + 2
}
|| Now break apart the first line, and make a string of the headers.
def end-of-line = {chars.find '\n'}
{if end-of-line >= 0 then
def first-line = {{chars.substr 0, end-of-line}.trim-clone}
def command-pieces = {first-line.split split-chars = ' '}
This is not top-level code but code in a procedure within a class so def is used without braces. The docs suggest the use of curly braces around a {def } expression when it is class-level or top-level so as to be distinct in appearance from local declarations. The code is easy to read, there are few type declarations and any later destructive assignment is precluded.
Note that in the case of
def (num-bytes-decoded, chars) =
the procedure {decode-characters } itself returns
(num-bytes-decoded:int, chars:String)
but such an expression might have returned more than two values with the surplus ignored by the def macro in its assignments.
The def of an object reference to an instance of a class can preclude bugs and undesired ad hoc "bug fixes" and keep code readable as well as facilitate testing.