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-Thread: 103376,1f8689f27f0e2d9a,start X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx01.iad01.newshosting.com!newshosting.com!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.freenet.de!news.n-ix.net!news.belwue.de!irazu.switch.ch!news-zh.switch.ch!switch.ch!cernne03.cern.ch!cern.ch!news From: Maciej Sobczak Newsgroups: comp.lang.ada Subject: Correct use of variants Date: Mon, 07 Nov 2005 11:32:33 +0100 Organization: CERN - European Laboratory for Particle Physics Message-ID: NNTP-Posting-Host: abpc10883.cern.ch Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Trace: sunnews.cern.ch 1131359553 3011 (None) 137.138.37.241 X-Complaints-To: news@sunnews.cern.ch User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20050922 Red Hat/1.7.12-1.1.3.2.SL3 X-Accept-Language: en-us, en Xref: g2news1.google.com comp.lang.ada:6264 Date: 2005-11-07T11:32:33+01:00 List-Id: Hi, Consider this: with Ada.Text_IO; with Ada.Integer_Text_IO; use Ada.Text_IO; use Ada.Integer_Text_IO; procedure Hello is type Chooser is (First, Second); type Discriminated(Which : Chooser) is record case Which is when First => X : Integer; when Second => Y : Integer; end case; end record; A : Discriminated := (Which => First, X => 7); begin Put(A.X); -- prints 7 New_Line; -- does not compile: -- A := (Which => Second, Y => 5); -- -- Put(A.Y); -- should print 5 -- New_Line; end Hello; Above, A is declared as a value of *constrained* type (what's the formal name for this - "anonymous constrained subtype"?), with the discriminant Which fixed to value First. Later the variant cannot be changed as shown in that the commented part would not compile. What is the correct way to achieve the declaration of A with the possibility of changing the discriminant later? One of the ways is to use *some* default value for the discriminant, but I might have difficulty deciding which value is better than the others for this role. An arbitrary choice would create a misleading message to the reader that the particular value is considered to be "best" or "idle" or "starting" or whatever. In addition, having a default value for the discriminant means that it's not obligatory to initialize the variable A while it's declared and I would like to keep this enforced. -- Maciej Sobczak : http://www.msobczak.com/ Programming : http://www.msobczak.com/prog/