|
Domains in ClassesFrom Visual Prolog 5.2 it is possible to declare domains inside classes both in the declaration of the class and in the implementation. Domains declared in a class declaration are public (protected) and can be used outside the class. The domains declared in a class implementation are private and can only be used only inside that implementation. Or to put it differently, domains declared in class declarations are global domains, while domains declared in a class implementation are local to that implementation. Notice that domains declared in non-abstract class declarations do not even have to be visible in the main module (if they are not really used in the main module) like traditional global domains have to (see Improved domains handling). Notice as well that a class domain (i.e. a domain declared as a class name) also does not have to be visible in the main module if the main module does not use this domain. It is straightforward to use domains declared in classes. The only thing that needs to be mentioned are scoping and qualification rules. As already mentioned, a domain declared in an implementation is local to that implementation, and inside this implementation the domain is used just like any other domain. A domain declared in a class declaration can be used directly in all classes that inherit (transitively) from that class (including the class itself). The domain can be used both in the declarations and in the implementations of these classes. In classes that do not inherit (transitively) from the class declaring the domain, the domain and all functors have to be qualified with the class name of the declaring class. The qualification has to refer to the class that really declares the domain, it cannot go to a class that merely inherits from that class. In other words, the derived classes only "inherit" the ability to use a domain. Notice that all domains declared in classes are static/class entities. The domain belongs to the class not to the individual objects (but like other static/class entities they can of course be used freely by objects). Thus if a class declares a domain this means that there will exist one domain, rather than one per object. Being able to declare domains in classes opens the possibility to use classes as modules. If a class only declares static/class entities, then it can be considered a module. Though it is possible to create objects corresponding to such a module (since they always have at least a default constructor), these objects are useless, since there are no methods on them. (Well, to be precise, such objects do have an identity, and this might be used for something, but let's not consider this very special aspect). The static/class entities of a class can, however, be used as ordinary global entities, as long as you remember to qualify them with the class name. One advantage of creating modules this way is that the module will have a separate name space (as the result of qualification with the class name). This means that you can choose names in the module more freely. It also ensures consistent naming of all entities in the module. Another advantage is that classes do not have to be included in the main module, even if they contain public domains (see Improved domains handling). |