This file shows a number of syntax equivalences in the compiler.
Because of the multiple syntax equivalences, some expressions can be written in many different ways. All of the following do the same thing and compile to the same code.
You could even start expanding out the equivalent of (1..10) which is really a shortcut for series(1, nil, 10)
. This could also be written 1.series(nil,10)
. This adds another 26 variations to the 13 variations above.
instead of writing: | you can write: |
{ arg x; x < 2 } | { |x| x < 2 } |
{ arg x = 123; x < 2 } | { |x = 123| x < 2 } |
{ arg x = 10.rand; x < 2 } | { |x = (10.rand)| x < 2 } or {|x(10.rand)| x < 2 } |
NOTE: When using the new ||
syntax, the default value needs to be enclosed in parenthesis if it's not a literal.
instead of writing: | you can write: |
{ |x| object.msg(a, x, b) } | object.msg(a, _, b) |
{ |x,y| object.msg(a, x, y) } | object.msg(a, _, _) |
{ |x| a + x } | a + _ |
{ |x| [a, b, x] } | [a, b, _] |
{ |x| (a: x) } | (a: _) |
NOTE: There are some limitations to the extent of the surrounding expression that
_
can capture. See
Partial Application for details.
instead of writing: | you can write: |
|
|
instead of writing: | you can write: |
f(x, y) | x.f(y) |
f(g(x)) | x.g.f |
instead of writing: | you can write: |
f.value(x) | f.(x) |
instead of writing: | you can write: |
Point.new(3, 4) | Point(3, 4) |
instead of writing: | you can write: |
p.x_(y) | p.x = y or x(p) = y |
instead of writing: | you can write: |
object.performList(\method, a, b, array) | object.method(a, b, *array) |
instead of writing: | you can write: |
if (x < 3, { \abc }, { \def }) | if (x < 3) { \abc } { \def } |
z.do({ |x| x.play }) | z.do { |x| x.play } |
while({ a < b }, { a = a * 2 }) | while { a < b } { a = a * 2 } |
Pfunc({ rrand(3, 6) }) | Pfunc { rrand(3, 6) } |
NOTE: Trailing arguments must be literal blocks. No other expression may be used as a trailing argument, even if it evaluates to a Function. For example, you cannot use a variable name as a trailing argument, even if this variable was assigned a Function.
Using a selector as an infix binary operator (discussed in the next section) enables a visually similar construct that does allow arbitrary expressions as operands, but these binary-operator constructs technically do not have trailing arguments.
A fairly common case when this syntactic restriction matters: a partial application using the _
syntax is an expression evaluating to a Function, but it is not a literal block. Therefore:
instead of writing: | you can write: |
div(x, y) | x div: y |
WARNING: When switching between various forms of call syntax, one has to be mindful that a selector as a binary operator has equal precedence with most other binary operators, but has lower precedence than the receiver dot notation (.
). Therefore, replacing a receiver syntax (dot) with a selector written as a binary operator can change the result of some expressions, as illustrated below:
Native infix operators like +
can also be written in (longer) function-call form, e.g.:
infix: | function call: |
1 + 2 | (+)(1, 2) |
The latter form is usually not a shortcut, except when one wants to dynamically change the adverb of an operator, for instance that of +++
, because adverbs in the infix notation are interpreted as literals.
instead of writing: | you can write: |
Set.new.add(3).add(4).add(5) | Set[3, 4, 5] |
Array[3, 4, 5] | [3, 4, 5] |
instead of writing: | you can write: |
z.at(i) | z[i] |
z.put(i, y) | z[i] = y |
instead of writing: | you can write: |
a.copyRange(4, 8) | a[4..8] |
a.copyToEnd(4) | a[4..] |
a.copyFromStart(4) | a[..4] |
instead of writing: | you can write: |
Array.series(16, 1, 1) or series(1, nil, 16) | (1..16) |
Array.series(6, 1, 2) or series(1, 3, 11) | (1, 3..11) |
There is also the similar syntax for creating an iterating Routine:
instead of writing: | you can write: |
seriesIter(1, 3, 11) | (:1, 3..11) |
As a simple (non-combinatorial) example, the following are equivalent ways of listing the first 10 primes:
instead of writing: | you can write: |
Event[\a -> 1, \b -> 2] | (a: 1, b: 2) |
instead of writing: | you can write: |
[\a, 1, \b, 2] | [a: 1, b: 2] |
instead of writing: | you can write: |
x = z.at(0); y = z.at(1); | # x, y = z; |
instead of writing: | you can write: |
'myName'.envirGet | ~myName |
'myName'.envirPut(9) | ~myName = 9 |
instead of writing: | you can write: |
'mySymbol' | \mySymbol |
NOTE: The shorthand only admits a subset of the symbols that may be enclosed in single quotes. See
Literals: Symbols for details.
instead of writing: | you can write: |
Ref.new(thing) | `thing |