var i = 1
# Collect undefined attributes
for npropdef in self.n_propdefs do
- if npropdef isa AAttrPropdef and npropdef.n_expr == null then
+ if npropdef isa AAttrPropdef and npropdef.n_expr == null and not npropdef.noinit then
v.write_attribute(npropdef.mpropdef.mproperty, recv, arguments[i])
i += 1
end
import modelbuilder
import literal
+redef class Prod
+ super ANode
+
+ # Try to get its single annotation with a given name
+ # If there is no such an annotation, null is returned.
+ # If there is more than one annotation, a error message is printed and the first annotation is returned
+ fun get_single_annotation(name: String, modelbuilder: ModelBuilder): nullable AAnnotation
+ do
+ var res = get_annotations(name)
+ if res.is_empty then return null
+ if res.length > 1 then
+ modelbuilder.error(res[1], "Error: multiple annotation `{name}`. A previous one is defined line {res[0].location.line_start}")
+ end
+ return res.first
+ end
+
+ # Return all its annotations of a given name in the order of their declaration
+ # Retun an empty array if no such an annotation.
+ fun get_annotations(name: String): Array[AAnnotation]
+ do
+ var res = new Array[AAnnotation]
+ var nas = n_annotations
+ if nas == null then return res
+ for na in nas.n_items do
+ if na.name != name then continue
+ res.add(na)
+ end
+ return res
+ end
+end
+
redef class AAnnotation
# The name of the annotation
fun name: String
module modelize_property
import modelize_class
+import annotation
redef class ToolContext
var modelize_property_phase: Phase = new ModelizePropertyPhase(self, [modelize_class_phase])
var mparameters = new Array[MParameter]
var anode: nullable ANode = null
for npropdef in nclassdef.n_propdefs do
- if npropdef isa AAttrPropdef and npropdef.n_expr == null then
+ if npropdef isa AAttrPropdef then
if npropdef.mpropdef == null then return # Skip broken attribute
+ var at = npropdef.get_single_annotation("noinit", self)
+ if at != null then
+ npropdef.noinit = true
+ if npropdef.n_expr != null then
+ self.error(at, "Error: `noinit` attributes cannot have an initial value")
+ end
+ continue # Skip noinit attributes
+ end
+ if npropdef.n_expr != null then continue
var paramname = npropdef.mpropdef.mproperty.name.substring_from(1)
var ret_type = npropdef.mpropdef.static_mtype
if ret_type == null then return
redef class AAttrPropdef
redef type MPROPDEF: MAttributeDef
+ # Is the node tagged `noinit`?
+ var noinit = false
+
# The associated getter (read accessor) if any
var mreadpropdef: nullable MMethodDef writable
# The associated setter (write accessor) if any
var i = 1
# Collect undefined attributes
for npropdef in self.n_propdefs do
- if npropdef isa AAttrPropdef and npropdef.n_expr == null then
+ if npropdef isa AAttrPropdef and not npropdef.noinit and npropdef.n_expr == null then
v.write_attribute(npropdef.mpropdef.mproperty, recv, args[i])
i += 1
end
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import kernel
+
+class A
+ var x: Object is noinit #alt1,3# var x: Object
+ var y: Object is noinit #alt2,3# var y: Object
+ fun work
+ do
+ if isset _x then x.output
+ if isset _y then y.output
+ 0.output
+ end
+ #alt5#var z: Object = 5 is noinit
+end
+
+var a: A
+a = new A#alt1#a = new A(2)#alt2#a = new A(30)#alt3#a = new A(4, 40)#alt4#a=new A(5)
+a.work
--- /dev/null
+alt/base_init_noinit_alt4.nit:30,3--10: Error: Incorrect number of parameters. Got 1, expected 0. Signature is
--- /dev/null
+alt/base_init_noinit_alt5.nit:26,23--28: Error: `noinit` attributes cannot have an initial value