One of the distinguishing features of BETA is the unification of abstraction mechanisms
like class, procedure, function, etc. into one general abstraction mechanisms: the pattern. In
the previous section we have discussed one aspect of patterns in the form of virtual
procedure patterns. Virtual procedure patterns correspond to using virtual patterns as virtual
A virtual class pattern is a virtual pattern used as a class. Virtual class pattern is the
BETA construct corresponding to class parameters of generic classes in Eiffel and template
classes in C++. Consider the following example:
The Set pattern has a virtual class pattern attribute, elm that is the pattern of elements to
be inserted into the set. Since elm is a virtual class pattern, it may be constrained in
subpatterns of Set. In the above declaration it is constrained (or qualified) by the pattern
Object. Since all patterns are subpatterns of Object, any pattern may be used for elm.
I.e. any object may be inserted into a Set object. The declaration of Set thus corresponds
In general pattern Set may be used as any other pattern in BETA, including as a super
Find:
(# name: ^text; E: ^elm
enter
name[]
do {find an element E, where E.name=name}
exit
E[]
#)
...
#)
In pattern PersonSet, elm has been constrained to pattern Person. Consequently only
instances of pattern Person may be inserted in a PersonSet. Within PersonSet, all
references qualified by elm are known to refer to instances of at least pattern Person. I.e.
it is possible to refer to Person attributes through such references. For example in pattern
Find, the expression E.name is legal since E must refer to a Person object.
Pattern PersonSet corresponds to a constrained generic class in Eiffel. In C++ this
kind of constrained genericity is not possible.
It is generally agreed that some form of parameterised class mechanism is needed for
statically-typed languages. C++ offers a simple form; Eiffel and BETA offer more advanced
forms. Whether or not the differences between the approaches taken in the latter languages
are important may be difficult to judge. In practice it seems that all three languages can
express the same. However, there is a difference with respect to the definition of
subclass/subtype/-conformance of such parameterised classes and in the handling of
covariance and contravariance. The latter will be further discussed in section 4.6 below. For
a further discussion of parameterised classes see Reference [MMM90].
Do'stlaringiz bilan baham: