Many languages provide built-in pointer and structural equality and comparison. To preserve encapsulation, in Sather these operations must go through the class interface like every method. The '=' symbol is syntactic sugar for a call to 'is_eq' (See Syntactic sugar expressions). 'is_eq:BOOL' must be explicitly defined by the type of the left side for this syntax to be useful.
The SYS class (See SYS defines a number of routines for accessing system information:) can be used to obtain equality based on pointer or structural notions of identity. This class also provides built-in mechanisms for comparison and hashing.
Example 14-4. Classes which define their own notion of equality should subtype from $IS_EQ. This class is a common parameter bound in container classes.
-- In standard library type $IS_EQ is is_eq(e:$OB): BOOL; end; |
Example 14-5. Many classes define a notion of equality which is different than pointer equality. For example, two STR strings may be equal although they are not unique.
class STR < $IS_EQ is ... is_eq(arg:$OB):BOOL is ... end; ... end |
Example 14-6. Many container classes need to be able to compute hash values of their items. Just as with 'is_eq', classes may subtype from $HASH to indicate that they know how to compute their own hash value. ID also provides this built-in hash function.
-- In standard library type $HASH is hash:INT; end |
Example 14-7. To preserve class encapsulation, Sather does not provide a built-in way to copy objects. By convention, objects are copied by a class-defined routine 'copy', and classes which provide this should subtype from $COPY.
-- In standard library type $COPY is copy:SAME; end |