comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic.brenta@insalien.org>
Subject: Re: Variant records..
Date: 16 Feb 2004 16:46:45 +0100
Date: 2004-02-16T16:46:45+01:00	[thread overview]
Message-ID: <m3n07jowm2.fsf@insalien.org> (raw)
In-Reply-To: e8f9995c.0402160725.45b85ca8@posting.google.com

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.



  reply	other threads:[~2004-02-16 15:46 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-02-15 22:51 Variant records Ganesh Ramasivan
2004-02-16 10:57 ` Preben Randhol
2004-02-16 15:25   ` Ganesh Ramasivan
2004-02-16 15:46     ` Ludovic Brenta [this message]
2004-02-16 16:16     ` Preben Randhol
2004-02-17 14:37       ` Ganesh Ramasivan
2004-02-17 15:43         ` Preben Randhol
2004-02-16 16:59   ` Ganesh Ramasivan
2004-02-16 20:14     ` Simon Wright
2004-02-17 14:56       ` Ganesh Ramasivan
2004-02-17 15:31         ` Robert I. Eachus
2004-02-16 23:17 ` tmoran
  -- strict thread matches above, loose matches on Subject: below --
1999-09-14  0:00 Variant Records Shawn Barber
1999-09-14  0:00 ` Matthew Heaney
1999-09-14  0:00 ` Mark Lundquist
1999-09-14  0:00 ` David C. Hoos, Sr.
1986-10-31  9:08 variant records Dean Luick
1986-11-03 14:17 ` Bev Sobelman
1986-11-05 16:35 ` stuart
1986-11-06 15:40 ` stt
replies disabled

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