order and the nesting of the control structures. The real value of the
expressions used has no effect on the control flow analyses.
-~~~
+~~~nitish
if true then
return
else
return
end
print 1 # Compile error: unreachable statement
+~~~
+~~~
if true then
return
end
## if
~~~
-if exp then stm
-if exp then stm else stm
+var exp = true
+# ...
+if exp then print 1
+if exp then print 2 else print 2
if exp then
- stms
+ print 1
+ print 2
end
if exp then
- stms
+ print 1
+ print 2
else if exp then
- stms
+ print 10
+ print 20
else
- stms
+ print 100
+ print 200
end
~~~
structure; then an error is signaled since a statement cannot begin with
`else`.
-~~~
-if exp then stm # OK: complete 'if' structure
-else stm # Syntax error: unexpected 'else'
+~~~nitish
+if exp then print 1 # OK: complete 'if' structure
+else print 2 # Syntax error: unexpected 'else'
~~~
## while
~~~
-while exp do stm
-while exp do
- stms
+var x = 0
+while x < 10 do x += 1
+print x # outputs 10
+
+while x < 20 do
+ print x # outputs 10 11 ... 19
+ x += 1
end
~~~
`for` declares an automatic variable used to iterates on `Collection` (`Array` and `Range` are both `Collection`).
~~~
-for x in [1..5] do print x # outputs 1 2 3 4 5
-for x in [1, 4, 6] do
- print x # outputs 1 4 6
+for i in [1..5] do print i # outputs 1 2 3 4 5
+for i in [1, 4, 6] do
+ print i # outputs 1 4 6
end
~~~
Step can also be used to specify the size of each increment at the end of a for cycle.
~~~
-for x in [9 .. 4].step(-1) do print x # outputs 9 8 7 6 5 4
-for x in [9 .. 4[.step(-2) do print x # outputs 9 7 5
+for i in [9 .. 4].step(-1) do print i # outputs 9 8 7 6 5 4
+for i in [9 .. 4[.step(-2) do print i # outputs 9 7 5
~~~
## loop
~~~
loop
- stms
+ print 1
if exp then break
- stms
+ print 2
end
~~~
~~~
do
- var x = 5
- print x
+ var j = 5
+ print j
end
-# x is not defined here
+# j is not defined here
~~~
## break, continue and label
`label` can be used with `break` or `continue` to act on a specific control structure (not necessary the current one). The corresponding `label` must be defined after the `end` keyword of the designated control structure.
~~~
-for i in [0..width[ do
- for j in [0..height[ do
- if foo(i, j) then break label outer_loop
+for i in [0..10[ do
+ for j in [0..10[ do
+ if i + j > 15 then break label outer_loop
+ print "{i},{j}"
# The 'break' breaks the 'for i' loop
end
end label outer_loop
~~~
do
- stmts
- if expr then break label block
- stmts
+ print 1 # printed
+ if exp then break label block
+ print 2 # not printed because exp is true
end label block
~~~
`assert` verifies that a given Boolean expression is true, or else it aborts. An optional label can be precised, it will be displayed on the error message. An optional `else` can also be added and will be executed before the abort.
~~~
-assert bla: whatever else
- # "bla" is the label
- # "whatever" is the expression to verify
+assert bla: exp else
+ # `bla` is the label
+ # `exp` is the expression to verify
print "Fatal error in module blablabla."
print "Please contact the customer service."
end
Variables are bound to values. A variable cannot be used unless it has a value in all control flow paths (à la Java).
~~~
-var x
-var y
-if whatever then
- x = 5
- y = 6
+var exp = 10
+# ...
+var a
+if exp > 0 then
+ a = 5
else
- x = 7
+ a = 7
end
-print x # OK
-print y # Compile error: y is possibly not initialized
+print a # OK
+~~~
+
+~~~nitish
+var b
+if exp > 0 then
+ b = 6
+end
+print b # Compile error: y is possibly not initialized
~~~
## Adaptive Typing
`or else`, `==`, `!=`, and `isa`).
~~~
-var x # a variable
-x = 5
+var c # a variable
+c = 5
# static type is Int
-print x + 1 # outputs 6
-x = [6, 7]
+print c + 1 # outputs 6
+c = [6, 7]
# static type is Array[Int]
-print x[0] # outputs "6"
+print c[0] # outputs "6"
+~~~
-var x
-if whatever then
- x = 5
+~~~
+# ...
+var d
+if exp > 0 then
+ d = 5
else
- x = 6
+ d = 6
end
# Static type is Int
+print d + 1
~~~
## Variable Upper Bound
An optional type information can be added to a variable declaration. This type is used as an upper bound of the type of the variable. When a initial value is given in a variable declaration without a specific type information, the static type of the initial value is used as an upper bound. If no type and no initial value are given, the upper bound is set to `nullable Object`.
+~~~nitish
+var e: Int # Upper bound is Int
+e = "Hello" # Compile error: expected Int
+
+var f = 5 # Upper bound is Int
+f = "Hello" # Compile error: expected Int
~~~
-var x: Int # Upper bound is Int
-x = "Hello" # Compile error: expected Int
-var y: Object # Upper bound is Object
-y = 5 # OK since Int specializes Object
-var z = 5 # Upper bound is Int
-z = "Hello" # Compile error: expected Int
-var t: Object = 5 # Upper bound is Object
-t = "Hello" # OK
+
+~~~
+var g: Object # Upper bound is Object
+g = 5 # OK since Int specializes Object
+
+var h: Object = 5 # Upper bound is Object
+h = "Hello" # OK
~~~
The adaptive typing flow is straightforward, therefore loops (`for`, `while`, `loop`) have a special requirement: on entry, the upper bound is set to the current static type; on exit, the upper bound is reset to its previous value.
-~~~
-var x: Object = ...
+~~~nitish
+var l: Object
# static type is Object, upper bound is Object
-x = 5
+l = 5
# static type is Int, bound remains Object
-while x > 0 do
+while l > 0 do
# static type remains Int, bound sets to Int
- x -= 1 # OK
- x = "Hello" # Compile error: expected Int
+ l -= 1 # OK
+ l = "Hello" # Compile error: expected Int
end
# static type is Int, bound reset to Object
-x = "Hello" # OK
+l = "Hello" # OK
~~~
## Type Checks
`isa` tests if an object is an instance of a given type. If the expression used in an `isa` is a variable, then its static type is automatically adapted, therefore avoiding the need of a specific cast.
~~~
-var x: Object = whatever
-if x isa Int then
- # static type of x is Int
- print x * 10 # OK
+var m: Object = 5
+# ...
+if m isa Int then
+ # static type of m is Int
+ print m * 10 # OK
end
~~~
Remember that adaptive typing follows the control flow, including the Boolean operators.
~~~
-var a: Array[Object] = ...
-for i in a do
+var n = new Array[Object]
+n.add(1)
+n.add(true)
+n.add("one")
+n.add(11)
+
+for i in n do
# the static type of i is Object
if not i isa Int then continue
# now the static type of i is Int
~~~
var max = 0
-for i in whatever do
+for i in n do
if i isa Int and i > max then max = i
# the > is valid since, in the right part
# of the "and", the static type of i is Int
end
+print max # outputs 11
~~~
Note that type adaptation occurs only in an `isa` if the target type is more specific that the current type.
~~~
-var a: Collection[Int] = ...
-if a isa Comparable then
+var col: Collection[Int] = [1, 2, 3]
+if col isa Comparable then
# the static type is still Collection[Int]
# even if the dynamic type of a is a subclass
# of both Collection[Int] and Comparable
- ...
+ # ...
end
~~~
`nullable` annotates types that can accept `null` or an expression of a compatible nullable static type.
~~~
-var x: nullable Int
-var y: Int
-x = 1 # OK
-y = 1 # OK
-x = null # OK
-y = null # Compile error
-x = y # OK
-y = x # Compile error
+var o: nullable Int
+var p: Int
+o = 1 # OK
+p = 1 # OK
+o = null # OK
+o = p # OK
+~~~
+
+~~~nitish
+p = null # Compile error
+p = o # Compile error
~~~
Adaptive typing works well with nullable types.
~~~
-var x
-if whatever then
- x = 5
+var q
+if exp > 0 then
+ q = 5
else
- x = null
+ q = null
end
-# The static type of x is nullable Int
+# The static type of q is nullable Int
~~~
Moreover, like the `isa` keyword, the `==` and `!=` operators can adapt the static type of a variable when compared to `null`.
~~~
-var x: nullable Int = whatever
-if x != null then
- # The static type of x is Int (without nullable)
- print x + 6
+var r: nullable Int = 10
+# ...
+if r != null then
+ # The static type of r is Int (without nullable)
+ print r + 6
end
-# The static type of x is nullable Int
+# The static type of r is nullable Int
~~~
And another example:
~~~
-var x: nullable Int = whatever
+var s: nullable Int = 10
+# ...
loop
- if x == null then continue
- # The static type of x is Int
+ if s == null then break
+ # The static type of s is Int
+ print s + 1
+
+ s = null
+ # The static type of s is null
end
~~~
`or else` can be used to compose a nullable expression with any other expression. The value of `x or else y` is `x` if `x` is not `null` and is `y` if `x` is null. The static type of `x or else y` is the combination of the type of `y` and the not null version of the type of `x`.
~~~
-var i: nullable Int = ...
-var j = i or else 0
-# the static type of j is Int (without nullable)
+var t: nullable Int = 10
+# ...
+var u = t or else 0
+# the static type of u is Int (without nullable)
~~~
Note that nullable types require a special management for [[attributes|attribute]] and [[constructors|constructor]].
`as` casts an expression to a type. The expression is either casted successfully or there is an `abort`.
~~~
-var x: Object = 5 # static type of x is Object
-print x.as(Int) * 10 # outputs 50
-print x.as(String) # aborts: cast failed
+var v: Object = 5 # static type of v is Object
+print v.as(Int) * 10 # outputs 50
+~~~
+
+~~~nitish
+print v.as(String) # aborts: cast failed
~~~
Note that `as` does not change the object nor does perform conversion.
~~~
-var x: Object = 5 # static type of x is Object
-print x.as(Int) + 10 # outputs "15"
-print x.to_s + "10" # outputs "510"
+var w: Object = 5 # static type of w is Object
+print w.as(Int) + 10 # outputs "15"
+print w.to_s + "10" # outputs "510"
~~~
Because of type adaptation, `as` is rarely used on variables. `isa` (sometime coupled with `assert`) is preferred.
`as(not null)` can be used to cast an expression typed by a nullable type to its non nullable version. This form keeps the programmer from writing explicit static types.
~~~
-var x: nullable Int = 5 # static type of x is nullable Int
-print x.as(not null) * 10 # cast, outputs 50
-print x.as(Int) * 10 # same cast, outputs 50
-assert x != null # same cast, but type of x is now Int
-print x * 10 # outputs 50
+var y: nullable Int = 5 # static type of y is nullable Int
+print y.as(not null) * 10 # cast, outputs 50
+print y.as(Int) * 10 # same cast, outputs 50
+assert y != null # same cast, but type of y is now Int
+print y * 10 # outputs 50
~~~
## Static Type Combination Rule
<!-- -->
~~~
-var d: Discrete = ...
+var dis: Discrete = 'a'
# Note: Int < Discrete < Object
-var x
-if whatever then x = 1 else x = d
+var z
+if exp > 0 then z = 1 else z = dis
# static type is Discrete
-if whatever then x = 1 else x = "1"
+if exp < 0 then z = 1 else z = "1"
# static type is nullable Object (upper bound)
-var a1 = [1, d] # a1 is a Array[Discrete]
+var a1 = [1, dis] # a1 is a Array[Discrete]
+~~~
+
+~~~nitish
var a2 = [1, "1"] # Compile error:
# incompatible types Int and String
~~~