From: Jean Privat Date: Fri, 26 Feb 2016 21:00:58 +0000 (-0500) Subject: modelize: new attribute annotation `is optional` X-Git-Url: http://nitlanguage.org modelize: new attribute annotation `is optional` Signed-off-by: Jean Privat --- diff --git a/src/frontend/check_annotation.nit b/src/frontend/check_annotation.nit index d640ac2..5adcabd 100644 --- a/src/frontend/check_annotation.nit +++ b/src/frontend/check_annotation.nit @@ -84,6 +84,7 @@ lazy noinit readonly writable +optional autoinit noautoinit lateinit diff --git a/src/modelize/modelize_property.nit b/src/modelize/modelize_property.nit index e61713d..6588268 100644 --- a/src/modelize/modelize_property.nit +++ b/src/modelize/modelize_property.nit @@ -203,7 +203,7 @@ redef class ModelBuilder mreadpropdef.mproperty.is_autoinit = true continue end - if npropdef.has_value then continue + if npropdef.has_value and not npropdef.is_optional then continue var msetter = npropdef.mwritepropdef if msetter == null then # No setter, it is a readonly attribute, so just add it @@ -212,6 +212,7 @@ redef class ModelBuilder if ret_type == null then return var mparameter = new MParameter(paramname, ret_type, false) mparameters.add(mparameter) + initializers.add(npropdef.mpropdef.mproperty) npropdef.mpropdef.mproperty.is_autoinit = true else @@ -1155,6 +1156,9 @@ redef class AAttrPropdef # Is the node tagged lazy? var is_lazy = false + # Is the node tagged optional? + var is_optional = false + # Has the node a default value? # Could be through `n_expr` or `n_block` var has_value = false @@ -1251,6 +1255,14 @@ redef class AAttrPropdef self.mlazypropdef = mlazypropdef end + var atoptional = self.get_single_annotation("optional", modelbuilder) + if atoptional != null then + if not has_value then + modelbuilder.error(atoptional, "Error: `optional` attributes need a default value.") + end + is_optional = true + end + var atreadonly = self.get_single_annotation("readonly", modelbuilder) if atreadonly != null then if not has_value then @@ -1422,9 +1434,13 @@ redef class AAttrPropdef var mwritepropdef = self.mwritepropdef if mwritepropdef != null then + var mwritetype = mtype + if is_optional then + mwritetype = mwritetype.as_nullable + end var name: String name = n_id2.text - var mparameter = new MParameter(name, mtype, false) + var mparameter = new MParameter(name, mwritetype, false) var msignature = new MSignature([mparameter], null) mwritepropdef.msignature = msignature end