SDL-News: Re: Casting to inheritance type


Subject: SDL-News: Re: Casting to inheritance type
From: Rick Reed TSE (rickreed#tseng.co.uk)
Date: Tue Jul 06 2004 - 16:27:30 GMT


Become an SDL Forum Society member <http://www.sdl-forum.org/Society/members.htm>
The originator of this message is responsible for its content.
-----From Rick Reed TSE <rickreed#tseng.co.uk> to sdlnews -----

Yeoh Chun Yeow at yeow#rndtm.net.my wrote on 06/07/04 08:12:

> newtype Unit inherits Integer
> endnewtype;
>
> So can I cast the Unit back to Integer?

Dear Yeoh Chun Yeow,

---------------------------------
[[ BTW: I assume that you realise you need
        to have "operators all" in

newtype Unit inherits Integer
   operators all
endnewtype;

(or some other specification of the ones to inherit)
otherwise no operators will be inherited,
just the literals. The "=" and "/=" operators
are defined for all data types.]]
---------------------------------

The operators in Integer are all defined with signatures
of the form:

xxxx (THIS Integer, THIS Integer) -> THIS Integer

or

yyyy (THIS Integer) -> THIS Integer

so the inherited operators have signatures of the form

xxxx (Unit, Unit) -> Unit
and
yyyy (Unit) -> Unit

because each "THIS Integer" is replaced by the
specialized type - in this case "Unit".

It has been considered to have a pair of operators, such as:

parentvalue(THIS Integer) -> Integer;
childvalue(Integer) -> THIS Integer;

For the Integer type itself these would do nothing.
But for types that inherit from Integer such as Unit
these could be used for conversion, because when inherited
for Unit (for example) the signatures would become:

parentvalue(THIS Unit) -> Integer;
childvalue(Integer) -> THIS Unit;

Currently such a mechanism is neither in the language
nor supported by the tool you are using.
 
Fortunately, what you can do is to convert a Unit via a Real number by:

DCL i Integer, r Real, u Unit;

r:=float(u);
i:=fix(r);

and in the other direction by

r:=float(i);
u:=fix(r);

The Real variable should not actually be needed,
and it should be possible to write:

u:= fix(float(i));
i:=fix(float(u));

Obviously the code generator not optimise these constructs,
so the SDL while functionally correct and portable may not
be efficient.

The alternative would be to write operators or procedures
that do the conversions, though this is also likely to
be inefficient - so the feature really does need to be
defined into the language. It should be done for all
Predefined types.
[[I will raise this again in the standards group]]

--
Rick Reed - rickreed#tseng.co.uk
Tel:+44 15394 88462 Mob.:+44 7970 50 96 50

--End text from Rick Reed TSE <rickreed#tseng.co.uk> to sdlnews --- For extra SDL Forum Society benefits join at <http://www.sdl-forum.org/Society/members.htm>



This archive was generated by hypermail 2a23 : Thu May 09 2013 - 16:05:50 GMT