comp.lang.ada
 help / color / mirror / Atom feed
From: Richard Riehle <richard@adaworks.com>
Subject: Re: Why is Ada NOT a good choice for a beginner to programming?
Date: Sat, 11 Jan 2003 04:09:37 -0800
Date: 2003-01-11T12:00:49+00:00	[thread overview]
Message-ID: <3E200981.DC3A4A37@adaworks.com> (raw)
In-Reply-To: avk4l9$19i$1@a1-hrz.uni-duisburg.de

Georg Bauhaus wrote:

> One complaint he had was that it be possible to define a Length
> using derived types and still have "*" return values of type Length,
> when it should return an area.  He had a point, hadn't he?
> OTOH, with only structural equivalence of types, and no way to derive
> both an integral length type and an integral area type, that are really
> diferent, how can you build areas into your types, such that the compiler
> will prevent use of values of the wrong type?
>
> In which languages has this been fixed?

We can still achieve compile-time safety using generic formal package
parameters.   The following code does not directly address the derived
type issue,  but it does illustrate a model that can be modified to
provide a solution to this problem.  In some ways, it is like putting
a wrapper around the data type, as suggested in the C++ example
from Hyman Rosen.

=================================================


-- ==========================================================
-- Using generic formal package parameters to ensure
-- type safety from compile-time checking.
-- Author:  Richard Riehle
-- ==========================================================
with Arithmetic_Operations;
with Adding_Operators;
with Multiplying_Operators;
procedure Arithmetic_Experiment is

  type Length is digits 7;  -- could be a derived type
  type Time   is digits 7;  -- could be a derived type
  type Mass   is digits 7; -- could be a derived type

  function "+" (L, R : Length) return Length'Base is
     Result : Length := 0.0;
  begin
     return Result; -- define algorithm for appropiate behavior;
  end "+";

  function "-" (L, R : Length) return Length'Base is
    Result : Length := 0.0;
  begin
     return Result; -- define algorithm for appropiate behavior;
  end "-";

  package Add_Length is new Adding_Operators
                       (Item => Length,
                        "+"  => Arithmetic_Experiment."+",
                        "-"  => Arithmetic_Experiment."-");
  package Add_Mass   is new Adding_Operators (Item => Mass);
  package Add_Time   is new Adding_Operators (Item => Time);

  package Multiply_Length is new Multiplying_Operators (Item => Length);
  package Multiply_Mass   is new Multiplying_Operators (Item => Mass);
  package Multiply_Time   is new Multiplying_Operators (Item => Time);

  package Length_Ops is new Arithmetic_Operations
                                                (Arithmetic => Length,
                                                  F1 => Add_Length,
                                                  F2 => Multiply_Length);

  package Mass_Ops   is new Arithmetic_Operations
                                                (Arithmetic => Mass,
                                                  F1 => Add_Mass,
                                                  F2 => Multiply_Mass);

  package Time_Ops   is new Arithmetic_Operations
                                                 (Arithmetic => Time,
                                                  F1 => Add_Time,
                                                  F2 => Multiply_Time);

begin

     null;

end Arithmetic_Experiment;

====================================================================


with Adding_Operators;
with Multiplying_Operators;
generic
  type Arithmetic is digits <>;
  with package F1 is new Adding_Operators      (Item => Arithmetic);
  with package F2 is new Multiplying_Operators (Item => Arithmetic);
package Arithmetic_Operations is end Arithmetic_Operations;

====================================================================

generic
  type Item is digits <>;
  with function "+" (L, R : Item) return Item is <>;
  with function "-" (L, R : Item) return Item is <>;
package Adding_Operators is end Adding_Operators;

========================================================

generic
  type Item is digits <>;
  with function "*" (L, R : Item) return Item is <>;
  with function "/" (L, R : Item) return Item is <>;
package Multiplying_Operators is end Multiplying_Operators;









  parent reply	other threads:[~2003-01-11 12:09 UTC|newest]

Thread overview: 119+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-12-21  3:30 Why is Ada NOT a good choice for a beginner to programming? David Wright
2002-12-21  5:44 ` sk
2002-12-23 14:29   ` Wes Groleau
2002-12-23 17:36     ` sk
2002-12-23 18:24       ` David Wright
2002-12-23 19:16         ` sk
2002-12-23 19:48           ` David Wright
2002-12-23 21:33             ` Wes Groleau
2002-12-23 21:17           ` Robert A Duff
2002-12-24 19:22             ` faust
2002-12-24  0:46               ` Robert A Duff
2002-12-24  9:41                 ` Pascal Obry
2002-12-25  4:43                 ` faust
2002-12-27 17:54                   ` Warren W. Gay VE3WWG
2003-01-02 13:50                     ` Wes Groleau
2002-12-25  7:24                 ` AG
2003-01-02  9:56                   ` John McCabe
2003-01-02 13:47                 ` Wes Groleau
2003-01-08 19:39                   ` faust
2003-01-08  2:44                     ` Jeffrey Creem
2003-01-08 18:54                       ` Wes Groleau
2003-01-08 19:48                         ` chris.danx
2003-01-08 21:59                           ` Wes Groleau
2003-01-09 15:31                             ` Georg Bauhaus
2003-01-10  3:26                               ` Hyman Rosen
2003-01-10 22:58                                 ` Georg Bauhaus
2003-01-10  3:51                               ` Kevin Cline
2003-01-10 13:29                               ` Wes Groleau
2003-01-11 12:09                               ` Richard Riehle [this message]
2003-01-11 18:13                                 ` Wes Groleau
2003-01-09 16:52                           ` faust
2003-01-08 21:57                             ` Vinzent Hoefler
2003-01-09  0:59                       ` faust
2003-01-09  0:59                       ` faust
2003-01-08 13:14                         ` Ted Dennison
2003-01-08 17:41                         ` Warren W. Gay VE3WWG
2003-01-09 16:40                           ` faust
2003-01-08 21:58                             ` Vinzent Hoefler
2002-12-23 21:30         ` Wes Groleau
2002-12-23 22:11           ` David Wright
2002-12-30 16:22         ` Mark
2002-12-24  0:50     ` Craig Carey
2002-12-29 14:44       ` Colin Paul Gloster
2002-12-21  8:39 ` arvids lemchens
2002-12-21  8:53 ` Gautier
2002-12-21  9:42 ` Craig Carey
2002-12-21 10:44 ` Dale Stanbrough
2002-12-21 15:47   ` Alfred Hilscher
2002-12-21 19:01     ` tmoran
2002-12-22  1:06       ` Robert C. Leif
2002-12-23 14:56         ` Wes Groleau
2002-12-23 11:05       ` Alfred Hilscher
2002-12-22  0:34     ` James S. Rogers
2002-12-23 11:11       ` Alfred Hilscher
2002-12-23 14:32         ` Wes Groleau
2002-12-23 19:54         ` tmoran
2002-12-21 10:51 ` Why is Ada a good choice for an ambitious beginner to programming Wojtek Narczynski
2002-12-22 14:40   ` Bill Findlay
2002-12-22 15:08     ` Bill Findlay
2002-12-22 23:02       ` David Wright
2002-12-23  1:36         ` Bill Findlay
2002-12-23  2:13           ` David Wright
2002-12-23 14:40           ` Wes Groleau
2002-12-27 21:25             ` Randy Brukardt
2002-12-23 11:52     ` Wojtek Narczynski
2002-12-23 14:12       ` Bill Findlay
2002-12-23 20:15         ` Wojtek Narczynski
2002-12-24 19:29           ` Bill Findlay
2002-12-24 22:56             ` Wojtek Narczynski
2002-12-24 23:31               ` Bill Findlay
2002-12-24 23:36               ` Adrian Knoth
2002-12-25  8:23             ` Hyman Rosen
2002-12-27 17:27               ` Kevin Cline
2002-12-28  1:56                 ` Bill Findlay
2002-12-28  2:13                   ` Bill Findlay
2002-12-29  6:07                   ` Hyman Rosen
2002-12-23 20:52         ` Wojtek Narczynski
2002-12-23 20:07     ` chris.danx
2003-01-02 10:04     ` John McCabe
2003-01-02 21:32       ` Bill Findlay
2003-01-04 22:55         ` David Wright
2003-01-05  0:50           ` Bill Findlay
2003-01-05  2:18             ` David Wright
2003-01-05  3:57               ` John R. Strohm
2003-01-10  2:51                 ` Richard Riehle
2003-01-10  9:45                   ` David Wright
2003-01-10 10:59                     ` Richard Riehle
2003-01-11 17:47                     ` u.r. faust
2003-01-06  9:43               ` John McCabe
2003-01-06 10:05         ` John McCabe
2003-01-06 11:13           ` Bill Findlay
2003-01-10  2:58           ` Richard Riehle
2003-01-10  9:29             ` John McCabe
2002-12-21 11:57 ` Why is Ada NOT a good choice for a beginner to programming? David Marceau
2002-12-21 15:59 ` Alfred Hilscher
2002-12-21 17:48 ` Britt Snodgrass
2002-12-21 20:05 ` Peter Hend�n
2002-12-22  1:54 ` faust
2002-12-22  1:28   ` Hyman Rosen
2002-12-22  2:11     ` Larry Kilgallen
2002-12-22 22:49     ` faust
     [not found] ` <u7m2ua.qk3.ln@beastie.ix.netcom.com>
2002-12-23 14:55   ` Wes Groleau
2002-12-24 14:01 ` W D Tate
2002-12-24 20:06   ` Rod Haper
2002-12-25  0:37   ` noone
2002-12-26 13:23     ` W D Tate
2002-12-26 20:37       ` David Starner
2002-12-27 14:02         ` W D Tate
2003-01-13 12:18 ` John English
  -- strict thread matches above, loose matches on Subject: below --
2002-12-21 15:47 Alexandre E. Kopilovitch
2002-12-23 14:48 ` Wes Groleau
2003-01-10  6:38 Grein, Christoph
2003-01-10  8:21 ` Hyman Rosen
2003-01-10 15:51 ` Kevin Cline
2003-01-10 19:00 ` Kevin Cline
2003-01-10  8:35 Grein, Christoph
2003-01-10  9:26 ` Hyman Rosen
2003-01-13 16:49 ` Hyman Rosen
2003-01-13  6:13 Grein, Christoph
replies disabled

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