[Oberon] Type compatibility of procedures

Diego Sardina dsar at eml.cc
Fri Nov 11 13:25:36 CET 2016


> If no, is there any reason why we didn't make
> these two procedure types compatible?

No, they aren't.

In Oberon the signature of a procedure doesn't follow
any kind of variance (contravariance, covariance or
invariance). Parameters of both procedure must have
the same type.

In Oberon-2 type-bound procedures instead force the
receiver to be type-invariant, i.e. the same of the
bounded type.

If you are using Oberon OOP style, the receiver will
be contravariant, i.e. it will accept a more general
argument.

This is usually not a problem, because it's safe to
have contravariant parameters. Instead in this case
you have to protect your procedure, so the receiver
will be type-invariant. Of course it will be caught
in run-time, not in compile time.

Assume you have something like this:

TYPE
   Figure = POINTER TO RECORD
     draw : PROCEDURE(f : Figure);
   END;

   Rectangle = POINTER TO RECORD
     (Figure)
     h, w : INTEGER;
   END;

(* Rectangle's draw method *)

PROCEDURE Draw (f : Figure);
BEGIN
   WITH f : Rectangle DO
     ...
   END
END Draw;

If you pass an object that is not at least of type
rectangle the program will halt. The fact that the
run-time will stop the program is a feature, this
is a precondition of the procedure.

If you want the receiver to be type-invariant and
forced by the language construct, use Oberon-2 or
Component Pascal.


--
Diego Sardina



More information about the Oberon mailing list