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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,499373c5a06cccc1 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2004-02-16 07:47:34 PST Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!news-out1.nntp.be!propagator4-cogent.newsfeed.com!propagator2-sterling!in.nntp.be!news.worldonline.be!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: Variant records.. Date: 16 Feb 2004 16:46:45 +0100 Organization: Worldonline Belgium Sender: lbrenta@lbrenta Message-ID: References: NNTP-Posting-Host: ppp-62-235-82-44.tiscali.be Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: news.worldonline.be 1076946452 25284 62.235.82.44 (16 Feb 2004 15:47:32 GMT) X-Complaints-To: abuse@worldonline.be NNTP-Posting-Date: Mon, 16 Feb 2004 15:47:32 +0000 (UTC) User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 Xref: archiver1.google.com comp.lang.ada:5599 Date: 2004-02-16T16:46:45+01:00 List-Id: ganesh.ramasivan@gdcanada.com (Ganesh Ramasivan) writes: > Preben Randhol wrote... > > On 2004-02-15, Ganesh Ramasivan wrote: > > > In the following example, how would i forbid the user of data type Foo > > > from specifying the following: > > > > > > John : Foo(Vehicle => MAN, Power => AUTOMATIC); > > > > > > ie. is there a way to restrict the user from specifying certain types > > > as variants? > > > > I guess this is homework? If it is why don't you simply try to do this > > and see what happens? > > This is just a simple example I am working with as I am an ada newbie. > > I tried and it compiles. What I am trying to do is to prevent the user > from creating types that do not make sense. IOW, you want to introduce a constraint between Vehicle and Power. Personally, I would replace the two discriminants with just one: type Vehicle is (Man, Bicycle, Car_Automatic, Car_Manual, Helicopter, Jet, Space_Launcher); The alternative would be to make type Foo private, hide the discriminants from the user, and provide a constructor function. This however replaces the static checks performed by the compiler with a run-time check, so your user needs a unit test wherever they call New_Foo. procedure Vehicles is package P is type Vehicle_Type is (Man, Bicycle, Car, Helicopter, Jet, Space_Launcher); type Power_Type is (Automatic, Manual, None); type Foo (<>) is private; function New_Foo (Vehicle : Vehicle_Type; Power : Power_Type) return Foo; private type Foo (Vehicle : Vehicle_Type; Power : Power_Type) is record Horsepower : Float; case Power is when Automatic | Manual => Gears : Positive; when others => null; end case; end record; end P; package body P is function New_Foo (Vehicle : Vehicle_Type; Power : Power_Type) return Foo is begin case Vehicle is when Car => if Power = None then raise Constraint_Error; end if; when others => if Power /= None then raise Constraint_Error; end if; end case; declare Result : Foo (Vehicle, Power); begin return Result; end; end New_Foo; end P; use P; F : Foo := New_Foo (Vehicle => Man, Power => Automatic); begin null; end Vehicles; -- Ludovic Brenta.