Merge: New `optional` annotation on attributes
authorJean Privat <jean@pryen.org>
Thu, 3 Mar 2016 07:35:09 +0000 (02:35 -0500)
committerJean Privat <jean@pryen.org>
Thu, 3 Mar 2016 07:35:09 +0000 (02:35 -0500)
As requested at #1843 by @R4PaSs, a new annotation `optional` is now available on attributes.

~~~nit
class A
    var x: Int = 99 is optional
   # Because of `optional`, the automatic signature of the A constructor is `init(x: nullable Int)`
end

var a = new A
print a.x # outputs 99
var b = new A(4)
print b.x # output 4
~~~

In the model, the `optional` annotation only affects the signature and the behavior of the setter.
It transforms the argument to a `nullable` one and use the provided value if `null` is given as a parameter.

The `nullable` parameter is then propagated to the automatic constructors in the usual way.

Basically, the previous example is equivalent to having written:

~~~
class A
    var x: Int is noautoinit
    fun x=(x: nullable Int) is autoinit do if x != null then self.x = x else self.x = 99
end
~~~

Pull-Request: #1965
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
Reviewed-by: Alexis Laferrière <alexis.laf@xymus.net>

1  2 
src/compiler/abstract_compiler.nit

Simple merge