1 # Local Variables and Static Typing
3 `var` declares local variables. In fact there is no global variable in Nit, so in this document *variable* always refers to a local variable. A variable is visible up to the end of the current
4 control structure. Two variables with the same name cannot coexist: no nesting nor masking.
6 Variables are bound to values. A variable cannot be used unless it has a value in all control flow paths (à la Java).
25 print b # Compile error: y is possibly not initialized
30 Nit features adaptive typing, which means that the static type of a variable can change according to: the assignments of variables, the control flow, and some special operators (`and`, `or`,
31 `or else`, `==`, `!=`, and `isa`).
37 print c + 1 # outputs 6
39 # static type is Array[Int]
40 print c[0] # outputs "6"
55 ## Variable Upper Bound
57 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`.
60 var e: Int # Upper bound is Int
61 e = "Hello" # Compile error: expected Int
63 var f = 5 # Upper bound is Int
64 f = "Hello" # Compile error: expected Int
68 var g: Object # Upper bound is Object
69 g = 5 # OK since Int specializes Object
71 var h: Object = 5 # Upper bound is Object
75 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.
79 # static type is Object, upper bound is Object
81 # static type is Int, bound remains Object
83 # static type remains Int, bound sets to Int
85 l = "Hello" # Compile error: expected Int
87 # static type is Int, bound reset to Object
93 `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.
99 # static type of m is Int
104 Remember that adaptive typing follows the control flow, including the Boolean operators.
107 var n = new Array[Object]
114 # the static type of i is Object
115 if not i isa Int then continue
116 # now the static type of i is Int
121 An interesting example:
126 if i isa Int and i > max then max = i
127 # the > is valid since, in the right part
128 # of the "and", the static type of i is Int
130 print max # outputs 11
133 Note that type adaptation occurs only in an `isa` if the target type is more specific that the current type.
136 var col: Collection[Int] = [1, 2, 3]
137 if col isa Comparable then
138 # the static type is still Collection[Int]
139 # even if the dynamic type of a is a subclass
140 # of both Collection[Int] and Comparable
147 `null` is a literal value that is only accepted by some specific static types. However, thanks to adaptive typing, the static type management can be mainly automatic.
149 `nullable` annotates types that can accept `null` or an expression of a compatible nullable static type.
161 p = null # Compile error
162 p = o # Compile error
165 Adaptive typing works well with nullable types.
174 # The static type of q is nullable Int
177 Moreover, like the `isa` keyword, the `==` and `!=` operators can adapt the static type of a variable when compared to `null`.
180 var r: nullable Int = 10
183 # The static type of r is Int (without nullable)
186 # The static type of r is nullable Int
192 var s: nullable Int = 10
195 if s == null then break
196 # The static type of s is Int
200 # The static type of s is null
204 `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`.
207 var t: nullable Int = 10
210 # the static type of u is Int (without nullable)
213 Note that nullable types require a special management for [[attributes|attribute]] and [[constructors|constructor]].
217 `as` casts an expression to a type. The expression is either casted successfully or there is an `abort`.
220 var v: Object = 5 # static type of v is Object
221 print v.as(Int) * 10 # outputs 50
225 print v.as(String) # aborts: cast failed
228 Note that `as` does not change the object nor does perform conversion.
231 var w: Object = 5 # static type of w is Object
232 print w.as(Int) + 10 # outputs "15"
233 print w.to_s + "10" # outputs "510"
236 Because of type adaptation, `as` is rarely used on variables. `isa` (sometime coupled with `assert`) is preferred.
239 var x: Object = 5 # static type of x is Object
241 # static type of x is now Int
242 print x * 10 # outputs 50
245 `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.
248 var y: nullable Int = 5 # static type of y is nullable Int
249 print y.as(not null) * 10 # cast, outputs 50
250 print y.as(Int) * 10 # same cast, outputs 50
251 assert y != null # same cast, but type of y is now Int
252 print y * 10 # outputs 50
255 ## Static Type Combination Rule
257 Adaptive typing, literal arrays, and `or else` need to determine a static type by combining other static types. This is done by using the following rule:
259 - The final type is `nullable` if at least one of the types is `nullable`.
261 - The final type is the static type that is more general than all the other types.
263 - If there is no such a type, and the thing typed is a variable, then the final type is the upper bound type of the variable; else there is a compilation error.
268 var dis: Discrete = 'a'
269 # Note: Int < Discrete < Object
271 if exp > 0 then z = 1 else z = dis
272 # static type is Discrete
273 if exp < 0 then z = 1 else z = "1"
274 # static type is nullable Object (upper bound)
275 var a1 = [1, dis] # a1 is a Array[Discrete]
279 var a2 = [1, "1"] # Compile error:
280 # incompatible types Int and String