1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Simple user-defined meta-level to manipulate types of instances as object.
17 # Unfortunately, since the meta-objects are user-defined they are provided without
18 # any pre-defined information or behavior.
19 # For the same reasons, the Nit OO mechanisms do not rely on this user-defined meta-level.
21 # However `meta` permits the definition of user-defined meta-classes at any level
22 # of meta, even with user-defined meta-loops.
26 # Meta-classes can be defined easily in 3 steps:
28 # * define a root for the class hierarchy (eg `XObject`)
29 # * define a meta-class (eq `XClass[E: XObject] super Class[E]`)
30 # * redefine `CLASS` and `class_factory` in the root
32 # ~~~nitish because broke with nitc, see Limitation bellow
34 # redef CLASS: XClass[SELF]
35 # redef class_factory(name) do return new XClass[SELF](name)
37 # class XClass[E: XObject] super Class[E] end
39 # var x1 = new XObject
40 # var x2 = new XObject
41 # assert x1.get_class == x2.get_class
42 # assert x1.get_class isa XClass[XObject]
43 # assert x1.get_class.get_class isa Class[XClass[XObject]]
48 # Currently works only with the interpreter `nit` and the compiler with `--erasure` (without `--rta`).
50 # `--rta` will try to detect all the runtime types, and will infinitely discover `Class[Class[Class[....]]]`.
51 # Unfortunately, `--separate` and `--global` require `--rta`.
53 # Moreover, the interpreter and `--erasure` have a different behavior with generics since
54 # with `--erasure` a single meta-instance is shared for all type variations of a same generic class.
56 # Class names are used as a primary key to identify classes.
57 # But name conflicts are not managed and will make the program crashes at runtime (on some cast error)
61 # The meta-object representing the dynamic type of `self`.
63 # Specific meta-object can be used in subclasses
64 # by redefining `CLASS` and `class_factory`.
65 fun get_class
: CLASS do
66 var class_pool
= once
new HashMap[String, Class[Object]]
68 var res
= class_pool
.get_or_null
(name
)
69 if res
!= null then return res
.as(CLASS)
70 res
= class_factory
(name
)
71 assert res
.name
== name
72 class_pool
[name
] = res
76 # The type of the class of self.
77 # To be redefined in case of specific meta-class.
78 type CLASS: Class[SELF]
80 # Implementation used by `get_class` to create the specific class.
82 # To be redefined to use specific meta-classes.
84 # Note: Do not forget to update the virtual type `CLASS`.
86 # REQUIRE: `result.name` == `name`
87 protected fun class_factory
(name
: String): CLASS
89 return new Class[SELF](name
)
93 # This meta-class is the root the meta-class hierarchy
94 class Class[E
: Object]
95 # The name of the class
98 redef fun to_s
do return name