What is meant by upcasting/expanding and downcasting/narrowing?
(Tucker Taft replies): Here is the symmetric case to illustrate upcasting and downcasting. type A is tagged …; — one parent type type B is tagged …; — another parent type … type C; — the new type, to be a mixture of A and B type AC (Obj : access C’Class) is new A with …; — an extension of A to be mixed into C type BC (Obj : access C’Class) is new B with …; — an extension of B to be mixed into C type C is tagged limited record A : AC (C’Access); B : BC (C’Access); … — other stuff if desired end record; We can now pass an object of type C to anything that takes an A or B as follows (this presumes that Foobar and QBert are primitives of A and B, respectively, so they are inherited; if not, then an explicit conversion (upcast) to A and B could be used to call the original Foobar and QBert). XC : C; …
Here is the symmetric case to illustrate upcasting and downcasting. type A is tagged …; — one parent type type B is tagged …; — another parent type … type C; — the new type, to be a mixture of A and B type AC (Obj : access C’Class) is new A with …; — an extension of A to be mixed into C type BC (Obj : access C’Class) is new B with …; — an extension of B to be mixed into C type C is tagged limited record A : AC (C’Access); B : BC (C’Access); … — other stuff if desired end record; We can now pass an object of type C to anything that takes an A or B as follows (this presumes that Foobar and QBert are primitives of A and B, respectively, so they are inherited; if not, then an explicit conversion (upcast) to A and B could be used to call the original Foobar and QBert). XC : C; … Foobar (XC.A); QBert (XC.B); If we want to override what Foobar does, then we override Foobar on AC. If we want to override what QBert does, then we override QBert on BC. Note that there are no n
(Tucker Taft replies): Here is the symmetric case to illustrate upcasting and downcasting. type A is tagged …; — one parent type type B is tagged …; — another parent type … type C; — the new type, to be a mixture of A and B type AC (Obj : access C’Class) is new A with …; — an extension of A to be mixed into C type BC (Obj : access C’Class) is new B with …; — an extension of B to be mixed into C type C is tagged limited record A : AC (C’Access); B : BC (C’Access); … — other stuff if desired end record; We can now pass an object of type C to anything that takes an A or B as follows (this presumes that Foobar and QBert are primitives of A and B, respectively, so they are inherited; if not, then an explicit conversion (upcast) to A and B could be used to call the original Foobar and QBert). XC : C; … Foobar (XC.A); QBert (XC.B); If we want to override what Foobar does, then we override Foobar on AC. If we want to override what QBert does, then we override QBert on BC. N