Example of Iterators
class MAIN is main is loop i := 1.upto!(10); #OUT + i + "\n"; end; end; end;This program prints numbers from 1 to 10.
The loop
... end
construct is the preferred means of defining loops (although while
and repeat
-until
are also available). Within the construct, one or more iterators may be used. Iterator names always end with an exclamation mark (this convention is enforced by the compiler). upto!
is a method of the integer class INT
accepting one once
argument, meaning its value will not change as the iterator yields. upto!
could be implemented in the INT
class like this:
Type information for variables is denoted by a postfix syntax variable:CLASS
. The type can often be inferred and thus the typing information is optional, like in anInteger::=1
. SAME
is a convenience pseudo-class referring to the current class.
Read more about this topic: Sather