From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,3885b7fd66a1db28 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-01-11 04:00:51 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!newsfeed.berkeley.edu!ucberkeley!cyclone-sf.pbi.net!151.164.30.35!cyclone.swbell.net!newsfeed1.easynews.com!easynews.com!easynews!newsfeed1.earthlink.net!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!stamper.news.atl.earthlink.net!harp.news.atl.earthlink.net!not-for-mail From: Richard Riehle Newsgroups: comp.lang.ada Subject: Re: Why is Ada NOT a good choice for a beginner to programming? Date: Sat, 11 Jan 2003 04:09:37 -0800 Organization: AdaWorks Software Engineering Message-ID: <3E200981.DC3A4A37@adaworks.com> References: <8ivo1vo5ir3piqsck4ondj0cuo47g426kf@4ax.com> <9gMS9.483613$GR5.189928@rwcrnsc51.ops.asp.att.net> Reply-To: richard@adaworks.com NNTP-Posting-Host: 41.b2.40.a1 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Server-Date: 11 Jan 2003 12:00:49 GMT X-Mailer: Mozilla 4.7 [en] (Win98; I) X-Accept-Language: en Xref: archiver1.google.com comp.lang.ada:32913 Date: 2003-01-11T12:00:49+00:00 List-Id: 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;