Files
ivy/doc/sample/Expressions.im

142 lines
3.2 KiB
Plaintext

y = 1 + 2 * 3 / 4 * 5 - 6 + 7 multiplyBy:2.
z = w = 2 + 3 multiplyBy:2.
x = (((1 + 2 * 3) multiplyBy:3) add: 5) + 2.
x = ((1 + 2 * 3) multiplyBy:3).
p = 5 multiply(by:3 add:(2 + 1)).
q = 10 squared squared.
p1
setAge:2 squared squared + 4 squared multiply(by:2 add:4 + 1 * 3)
in:"mon" + "ths".
M = xz multiply(by:3 add:2) squared.
M = xz multiply(by:3 add:2); squared.
x = OrderedCollection new
add: 2;
add: 4;
add: 6;
yourself.
age = Person new(name:"John Doe" age:34)
setAge:144 inUnit:"months";
ageInMonths.
x = 5.
q = 10 if x > 2.
q = if x > 2 then 10 else 20 end.
cout put:5 multiply(by:5 add:2) if x > 2.
if x > 2 then
cout put:"Greater"
else
cout put:"Less"
end
Q = match yz in
1 => age = age,
2 => age = age / 12,
3 => age = age / 365,
_ => age = 0
end.
while x > 2 do
cout put:"message"
end
for i in 0 to:100 step:2 do
cout put:'Count is {i}'
end
cout put:"message" while x > 2.
cout put:'Count is {i}' for i in 0 to:100 step:2.
z = do
x = 1.
y = 2.
^x + y
end.
class Person
/**
every message handler can have one or both of the following:
- a message name (in this case, 'init')
- a set of one or more parameters (wrapped in parentheses if the message
has a name)
each parameter has an internal name and an (optional) label, specified
as a pair of identifiers separated by a colon (:). the label comes
first, and is the name used by whoever is sending the message the
internal name comes second, and is used by the message handler itself as
a variable name.
you can omit the label for a parameter by specifying an underscore (_)
as the label, but this is not recommended. it looks weird, it obscures
the meaning of the message, and is really only supported for
compatibility with lambdas.
**/
- init(name:name age:age)
self::name = name.
self::age = age!
- test(param:data _:extra) | cout put:'Received {data}, {extra}'.
- name | ^self::name.
- age | ^self::age.
- ageInMonths | ^self::age * 12.
- setName:name | self::name = name.
- setAge:age | self::age = age.
- setAge:age inUnit:units
match units in
#years => self::age = age,
#months => self::age = age / 12,
#days => self::age = age / 365,
_ => self::age = 0
end!
- getAgeInUnit:units
^match units in
#years => self::age,
#months => self::age / 12,
#days => self::age / 365,
_ => 0
end!
$ exampleProperty | get => self::val, set => self::val = value.
$ exampleProperty2 |
get => [ ^self::val ],
set => [ :x | self::val = x ].
$ exampleProperty3 | get => 42.
$ exampleProperty4 (get, set)
$ exampleProperty5 (get)
end
j = [ cout put:'Hello!' ].
k = [ :x :y | cout put:'Hello, {x}, {y}!' ].
x > 2
if:[ cout put:'True' ]
else:[ cout put: 'False' ].
0 to:100 step:2 do:[ :i | cout put:'Count: {i}' ].
p1 test(param:'Hello' :'World').
pkg = {}.
pkg = { 0 }.
pkg = { 1, 2, 3, 4, 5 }.
pkg = { 10, 2 => "Hello", 9, 5 => "World", 14 }.
pkg = { { 0 }, { 1 }, { y for y in huh } }.
pkg = { x multiplyBy:2 for x in wow if x > 2 }.