Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

What is meant by upcasting/expanding and downcasting/narrowing?

0
Posted

What is meant by upcasting/expanding and downcasting/narrowing?

0

(Tucker Taft replies): Here is the symmetric case to illustrate upcasting and downcasting.

0

(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; …

0

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

0

(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

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.