comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Question on types conversions - operations
Date: Sat, 13 Jun 2009 18:58:40 +0200
Date: 2009-06-13T18:58:41+02:00	[thread overview]
Message-ID: <1jbptobad4t8l$.wfr99mkqhovj$.dlg@40tude.net> (raw)
In-Reply-To: 4a33cb0b$0$2848$ba620e4c@news.skynet.be

On Sat, 13 Jun 2009 17:51:17 +0200, Olivier Scalbert wrote:

> I have the following three types:
>      type Length is new Float;
>      type Time   is new Float;
>      type Speed  is new Float;
> 
> Suppose I need to compute a speed given a length and a time.
> 
>      L: Length := 100.0;
>      T: Time   :=  10.0;
>      S: Speed;
> 
> How to do that ?
> 
> With S := Speed(Float(L) / Float(T)) ? Mmmm ... not nice.
> 
> With S := Speed(L / Length(T)), I am also not satisfy as I convert a 
> time in Length ...

Once you have said 'A', you have to say 'B':

function "/" (Left, Right : Length) return Length is abstract;
function "/" (Left, Right : Time) return Length is abstract;
function "/" (Left, Right : Speed) return Length is abstract;
function "/" (Left : Length; Right : Time) return Speed;

The first three disallow illegal operations on Length, Time and Speed.

The body of "/" is

function "/" (Left : Length; Right : Time) return Speed is
begin
   return Speed (Float (Left) / Float (Right));
end "/";

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  parent reply	other threads:[~2009-06-13 16:58 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-06-13 15:51 Question on types conversions - operations Olivier Scalbert
2009-06-13 16:13 ` Robert Matthews
2009-06-13 16:55 ` anon
2009-06-13 16:58 ` Dmitry A. Kazakov [this message]
2009-06-13 16:59 ` sjw
2009-06-13 17:24   ` Martin
2009-06-13 19:35     ` sjw
2009-06-14  8:22     ` sjw
2009-06-15  8:40   ` Jean-Pierre Rosen
2009-06-15  9:30     ` Olivier Scalbert
2009-06-15  9:51       ` stefan-lucks
2009-06-15 10:33     ` AdaMagica
2009-06-15 19:37     ` sjw
2009-06-13 19:56 ` Jeffrey R. Carter
2009-06-14 15:55   ` Robert A Duff
2009-06-15  8:52 ` AdaMagica
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox