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,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.202.84.5 with SMTP id i5mr9753244oib.6.1505277251736; Tue, 12 Sep 2017 21:34:11 -0700 (PDT) X-Received: by 10.36.163.194 with SMTP id p185mr77803ite.14.1505277251444; Tue, 12 Sep 2017 21:34:11 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!feeder.erje.net!1.eu.feeder.erje.net!feeder.usenetexpress.com!feeder-in1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!o200no660985itg.0!news-out.google.com!c139ni385itb.0!nntp.google.com!127no661844itw.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Tue, 12 Sep 2017 21:34:11 -0700 (PDT) In-Reply-To: <1003d4d6-dfb0-43c0-93fc-dd81e56e5e8c@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=173.71.208.22; posting-account=QF6XPQoAAABce2NyPxxDAaKdAkN6RgAf NNTP-Posting-Host: 173.71.208.22 References: <364ff8e0-c7dd-4980-b19f-5d438edd8353@googlegroups.com> <7df81b3c-1fde-4395-8b9b-1a945820a7f7@googlegroups.com> <4565e696-3294-4c27-a761-d18eccb1e1e1@googlegroups.com> <1003d4d6-dfb0-43c0-93fc-dd81e56e5e8c@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: Type invariants and private extensions? From: Jere Injection-Date: Wed, 13 Sep 2017 04:34:11 +0000 Content-Type: text/plain; charset="UTF-8" Xref: news.eternal-september.org comp.lang.ada:48095 Date: 2017-09-12T21:34:11-07:00 List-Id: 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: > > > > > > > > > > Thanks. > > > > > > But this seems not to solve my problem: > > > > > > > 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;