next up previous contents
Next: 6.12 Controlling the interface Up: 6.11 Classes Previous: 6.11.2 Inheritance   Contents

6.11.3 Minor class features

There are a couple of other things you can do with classes. You can define a special member called _check. If this member is defined, then when a class instance is created, the check member is returned instead of the class itself. You can use this to implement class argument type checks, for example:

                                                      
  Fred a b = class {
    _check
      = this, is_real a && is_real b
      = error "args to Fred must " ++
        "both be real"
  }

Defines a class called Fred which has to have two real numbers as arguments.

You can define members called oo_binary, oo_binary' and oo_unary and do operator overloading. When nip2 sees one of the standard operators being used on an instance of your class, it will look up one of these members and pass in the name of the operator and the argument. The two forms of the binary operator member are called for the class-on-left and the class-on-rights cases. So:

                                                      
  x = Fred 1 2
  x + 12 == x.oo_binary "add" 12
  12 + x == x.oo_binary' "add" 12
  !x == x.oo_unary "negate"

These two features are very primitive. The _Object class in the _types toolkit builds on these to provide a fairly high-level system for checking class arguments and defining the meaning of operators. See §6.13.


next up previous contents
Next: 6.12 Controlling the interface Up: 6.11 Classes Previous: 6.11.2 Inheritance   Contents
John Cupitt 2004-12-20