Merge: doc: fixed some typos and other misc. corrections
[nit.git] / tests / test_autoinit_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 # This test provides variations on having an attribute with a default value but optionally settable with the constructor
16 # The main points of variation here is what are the basic exposed services and behavior
17
18 # The attribute is stored as not nullable but manual setter with a nullable signature is exposed
19 # Setting with `null` invoke the real setter with the default value.
20 # The manual setter is used as an initializer thus collected in the autoinit.
21 #
22 # Best option if one wants to allow the user to reset the default (with null) once the object is created
23 class A
24 var s: String is noautoinit, private writable(real_s=)
25 fun s=(v: nullable String) is autoinit do self.real_s = v or else "Default"
26 end
27
28 # The attribute is stored as not nullable with standard automatic getter/setter
29 # The initializer is a manual method that accepts the nullable value and assigns the default value if null is given.
30 #
31 # Best option if the default can only be set a construction time
32 class B
33 var s: String is noautoinit
34 protected fun s_opt=(v: nullable String) is autoinit do self.s = v or else "Default"
35 end
36
37 # The attribute is stored as nullable.
38 # A manual getter is used to return a default value if the attribute is null.
39 #
40 # Shortest solution without annotation.
41 # Con: the default is determined by the getter and never stored (this could be an issue if the default is complex)
42 class C
43 protected var s_opt: nullable String
44 fun s: String do return s_opt or else "Default"
45 end
46
47 # The attribute is stored as nullable.
48 # A manual getter is used to return a default value if the attribute is null.
49 #
50 # Longer solution without annotation but the getter lazily computes and stores the default once
51 class D
52 protected var s_opt: nullable String
53 fun s: String do
54 var res = s_opt
55 if res != null then return res
56 res = "Default"
57 s_opt = res
58 return res
59 end
60 end
61
62 # The attribute is stored as not nullable with standard automatic getter/setter
63 # The initializer is manual method that accepts the nullable value and does nothing if null is given.
64 #
65 # This one has a tricky behavior if one accesses to `s` before `s_opt=` is executed
66 class E
67 var s = "Default" is lazy
68 protected fun s_opt=(v: nullable String) is autoinit do if v != null then s = v
69 end
70
71 var a2 = new A("Mine")
72 print a2.s
73 var a1 = new A
74 print a1.s
75 a1.s = "Other"
76 print a1.s
77 a1.s = null
78 print a1.s
79
80 print ""
81
82 var b2 = new B("Mine")
83 print b2.s
84 var b1 = new B
85 print b1.s
86 b1.s = "Other"
87 print b1.s
88 b1.s_opt = null
89 print b1.s
90 b1.s_opt = "Yet Another"
91 print b1.s
92
93 print ""
94
95 var c2 = new C("Mine")
96 print c2.s
97 var c1 = new C
98 print c1.s
99 c1.s_opt = "Other"
100 print c1.s
101 c1.s_opt = null
102 print c1.s
103
104 print ""
105
106 var d2 = new D("Mine")
107 print d2.s
108 var d1 = new D
109 print d1.s
110 d1.s_opt = "Other"
111 print d1.s
112 d1.s_opt = null
113 print d1.s
114
115 print ""
116
117 var e2 = new E("Mine")
118 print e2.s
119 var e1 = new E
120 print e1.s
121 e1.s = "Other"
122 print e1.s
123 e1.s_opt = null
124 print e1.s
125 e1.s_opt = "Yet Another"
126 print e1.s