comp.lang.ada
 help / color / mirror / Atom feed
From: Jere <jhb.chat@gmail.com>
Subject: Re: Type invariants and private extensions?
Date: Tue, 12 Sep 2017 21:34:11 -0700 (PDT)
Date: 2017-09-12T21:34:11-07:00	[thread overview]
Message-ID: <d433ff09-ac18-4f4d-9eeb-7f0f15c87224@googlegroups.com> (raw)
In-Reply-To: <1003d4d6-dfb0-43c0-93fc-dd81e56e5e8c@googlegroups.com>

On Wednesday, September 13, 2017 at 12:28:10 AM UTC-4, Jere wrote:
> On Wednesday, September 13, 2017 at 12:21:12 AM UTC-4, Jere wrote:
> > On Tuesday, September 12, 2017 at 6:59:17 PM UTC-4, Victor Porton wrote:
> > > Shark8 wrote:
> > > 
> > > > On Tuesday, September 12, 2017 at 2:09:08 PM UTC-6, Victor Porton wrote:
> > > >> Jeffrey R. Carter wrote:
> > > >> 
> > > >> > 
> > > >> > "I want to" is not the same as "there is no way to solve the problem in
> > > >> > current Ada". Ada has a feature that provides exactly what you need.
> > > >> > It's called a variant record.
> > > >> 
> > > >> It is a tagged type. AFAIK, a type cannot be both a variant record and
> > > >> tagged.
> > > > 
> > > > You certainly can:
> > > > <SNIPPED>
> > > 
> > > Thanks.
> > > 
> > > But this seems not to solve my problem:
> > > <SNIPPED>
> > 
> > I may be misunderstanding what you are looking for, but you can 
> > make discriminant types unconstrained to let you choose the type 
> > at run time:
> > 
> > Type Type_Enumeration is ( TInteger, TReal, TBoolean, Nothing );
> >    
> >     Type Example( Item_Type : Type_Enumeration := Nothing) is tagged record
> >         case Item_Type is
> >             when TInteger => I : Integer;
> >             when TReal    => R : Float range Float'Range;
> >             when TBoolean => B : Boolean;
> >             when Nothing  => Null;
> >         end case;
> >     end record; 
> > 
> > Then all you have to do is declare an object with no 
> > specified discriminant:
> > 
> >  Some_Object : Example;  --notice no discriminant
> > 
> > In Ada, when you do this, you are able to assign new versions 
> > of the type (with different discriminants:
> > 
> > Some_Integer : Example := (Item_Type => TInteger, I => 100);
> > Some_Float   : Example := (Item_Type => TFloat,   F => 20.2);
> > 
> > Some_Object := Some_Integer;
> > Some_Object := Some_Float;
> > 
> > That's just a play example, you can handle building them however 
> > you want at run time.
> 
> Sorry, TFloat should be TReal and F should be R
> 
> Also, I didn't realize this, but you can't do the default value with a tagged type unless it is limited so it would need to be untagged:
> 
>    Type Example( Item_Type : Type_Enumeration := Nothing) is record
>         case Item_Type is
>             when TInteger => I : Integer;
>             when TReal    => R : Float range Float'Range;
>             when TBoolean => B : Boolean;
>             when Nothing  => Null;
>         end case;
>     end record; 
> 
> If you are selecting the type via enum, does it still need to be tagged at this point?

If you do need it tagged, you can also wrap the non tagged record in a tagged record:

   type Wrapper is tagged record
      Placeholder : Example;  -- notice no discriminant
   end record;
   
   Some_Object : Wrapper;


   Some_Integer : Wrapper := (Placeholder => (Item_Type => TInteger, I => 100));
   Some_Float   : Wrapper := (Placeholder => (Item_Type => TReal,    R => 20.2)); 

and then later:

   Some_Object := Some_Integer;
   Some_Object := Some_Float; 


  reply	other threads:[~2017-09-13  4:34 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-11 19:51 Type invariants and private extensions? Victor Porton
2017-09-11 20:00 ` Egil H H
2017-09-11 20:48   ` Victor Porton
2017-09-11 21:19     ` Egil H H
2017-09-11 21:27       ` Victor Porton
2017-09-11 21:49         ` Egil H H
2017-09-11 22:00           ` Victor Porton
2017-09-11 22:06             ` Egil H H
2017-09-12  7:30             ` Dmitry A. Kazakov
2017-09-11 22:00         ` Jere
2017-09-11 22:02           ` Victor Porton
2017-09-12 18:26             ` Jeffrey R. Carter
2017-09-12 18:54               ` Victor Porton
2017-09-12 19:56                 ` Jeffrey R. Carter
2017-09-12 20:08                   ` Victor Porton
2017-09-12 22:34                     ` Shark8
2017-09-12 22:59                       ` Victor Porton
2017-09-13  4:21                         ` Jere
2017-09-13  4:28                           ` Jere
2017-09-13  4:34                             ` Jere [this message]
2017-09-14 13:52                           ` Victor Porton
2017-09-15  0:48                             ` Jere
2017-09-16 14:22                               ` Victor Porton
2017-09-14  7:28                         ` Shark8
2017-09-14 13:56                           ` Victor Porton
2017-09-14 13:58                             ` Victor Porton
2017-09-11 22:48         ` Shark8
2017-10-02 23:16         ` Randy Brukardt
replies disabled

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