[comp.lang.c] Typing on units

sommar@enea.se (Erland Sommarskog) (03/15/90)

Marvin Rubenstein (marv@ism780.UUCP) writes:
>I am unaware of any commonly available language that prevents this form of
>mistake.  Look at the following:
>
>  double distance;
>  double time;
>  double velocity;
>
>  velocity = distance/time;  /* this makes sense */
>  velocity = distance+time;  /*  I mixed 'apples' and 'oranges' and produced
>	        		 a lemon :-) */

The program below contains the same error and does not compile.
I have edited the error messages to stay below 80 columns.

   PROCEDURE Type_test IS
   
       TYPE Meter             IS NEW Float;
       TYPE Seconds           IS NEW Float;
       TYPE Meter_per_seconds IS NEW Float;

       x : Meter   := 2.2;
       s : Seconds := 3.3;
       v : Meter_per_seconds;

       FUNCTION "/"(x : Meter; s : Seconds) RETURN Meter_per_seconds IS
       BEGIN
          RETURN Meter_per_seconds(x) / Meter_per_seconds(s);
       END "/";

   BEGIN
      v := x / s;
      v := x + s;
   --------^A
   ------------^B
   --A:error: RM 3.3: base type of expression must be meter_per_seconds, line 5
   --B:error: RM 3.3: base type of expression must be meter_per_seconds, line 5
   END Type_test;

In order to get division to work we had to define a function for it. 
In fact we have to define a function of a similar kind for every operation
we want to make. This is a lot of typing, on the other hand: it does
well document the rules at hand. Performance won't be an issue. In 
the sample above, the subprogram would disappear rapidly in the hands
of the optimizer. Normally it would be part of a package, but you
would mention in it in the INLINE pragma.
-- 
Erland Sommarskog - ENEA Data, Stockholm - sommar@enea.se

gwollman@jhunix.HCF.JHU.EDU (Garrett A Wollman) (03/16/90)

Since noone else has yet complained about this (visibly to me, anyhow),
COULD ALL OF YOU PLEASE TAKE THIS ADA GARBAGE OUT OF comp.lang.c.
                                                          ^^^^^^^

I paraphrase from the description of active newsgroups:

comp.lang.c	For discussion of issues related to the C programming lang.
comp.lang.ada	For discussion of issues related to the Ada(tm) language.

Now I can remove that irritating /ada/a:j from my KILL file.

-GAWollman
"Sorry for the networked flame"

-- 
"All societies are based on rules to protect pregnant women and children.
 . . . As racial survival is the only universal morality, no other bases
 is possible."           - Lazarus Long [RAH, _TEFL_]
---------------Hopkins doesn't *want* my opinions------------------------