Dear All, TYPE Class = POINTER TO ClassDesc; ClassDesc = RECORD x : INTEGER; method : PROCEDURE (self: Class; v : INTEGER); END; Subclass = POINTER TO SubclassDesc; SubclassDesc = RECORD (ClassDesc) y : INTEGER END; VAR ptr : Class; subPtr : SubClass; If we have two procedures like this: PROCEDURE Method (self: Class; v : INTEGER); BEGIN self.x := v END Method; PROCEDURE OverridingMethod (self: Subclass; v : INTEGER); BEGIN self.x := v; self.y := v END OverridingMethod; We can do, NEW(subPtr); subPtr.method := Method; Can't we do, NEW(subPtr); subPtr.method := OverridingMethod; ? If no, is there any reason why we didn't make these two procedure types compatible? PROCEDURE Method (self: Class; v : INTEGER); PROCEDURE OverridingMethod (self: Subclass; v : INTEGER); With thanks and best regards, Yours sincerely, Srinivas Nayak Home: http://www.mathmeth.com/sn/ Blog: http://srinivas-nayak.blogspot.in/
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
participants (2)
-
Diego Sardina -
Srinivas Nayak