Merge: doc: fixed some typos and other misc. corrections
[nit.git] / tests / base_is_optional.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import core::kernel
16
17 fun foo(i: Int): Int
18 do
19 'f'.output
20 i.output
21 return i
22 end
23
24 class A
25 # needed on the new
26 var i: Int
27
28 # initialized by the allocation
29 var j: Int = foo(2)
30
31 # optional in the new, default value evaluated if `null` is given
32 var k: Int = foo(3) is optional
33
34 # the `init` will initialize it
35 var l: Int is noautoinit
36 init do l = foo(4)
37
38 # initialized if needed on the first `read`
39 var m: Int = foo(5) is lazy
40
41 fun set
42 do
43 i = 10
44 j = 20
45 k = 30
46 l = 40
47 m = 50
48 end
49
50 fun test
51 do
52 #alt1#set
53 i.output
54 j.output
55 k.output
56 l.output
57 m.output
58 '\n'.output
59 end
60 end
61
62 var a
63 a = new A(foo(100))
64 a.test
65 a = new A(foo(100), null)
66 a.test
67 a = new A(foo(100), foo(300))
68 a.test