typing: `intern` is used to refer to the plain vanilla constructor
authorJean Privat <jean@pryen.org>
Fri, 10 Apr 2015 05:16:03 +0000 (12:16 +0700)
committerJean Privat <jean@pryen.org>
Fri, 10 Apr 2015 05:16:03 +0000 (12:16 +0700)
It is used to implement specific factories inside classes

~~~
class A
   new do
      var res = new A.intern
      res.do_things
      return res
   end
end
~~~

Signed-off-by: Jean Privat <jean@pryen.org>

src/semantize/typing.nit

index cf8bbda..93ed60d 100644 (file)
@@ -1809,6 +1809,7 @@ redef class ANewExpr
                end
 
                self.recvtype = recvtype
+               var kind = recvtype.mclass.kind
 
                var name: String
                var nid = self.n_id
@@ -1817,11 +1818,24 @@ redef class ANewExpr
                else
                        name = "new"
                end
+               if name == "intern" then
+                       if kind != concrete_kind then
+                               v.error(self, "Type Error: Cannot instantiate {kind} {recvtype}.")
+                               return
+                       end
+                       if n_args.n_exprs.not_empty then
+                               v.error(n_args, "Type Error: the intern constructor expects no arguments.")
+                               return
+                       end
+                       # Our job is done
+                       self.mtype = recvtype
+                       return
+               end
+
                var callsite = v.get_method(self, recvtype, name, false)
                if callsite == null then return
 
                if not callsite.mproperty.is_new then
-                       var kind = recvtype.mclass.kind
                        if kind != concrete_kind then
                                v.error(self, "Type Error: Cannot instantiate {kind} {recvtype}.")
                                return