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 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!novia!newsfeed.icl.net!proxad.net!proxad.net!oleane.net!oleane!hunter.axlog.fr!nobody From: Jean-Pierre Rosen Newsgroups: comp.lang.ada Subject: Re: Correct use of variants Date: Mon, 07 Nov 2005 13:52:32 +0100 Organization: Adalog Message-ID: References: NNTP-Posting-Host: mailhost.axlog.fr Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Trace: s1.news.oleane.net 1131368485 6374 195.25.228.57 (7 Nov 2005 13:01:25 GMT) X-Complaints-To: abuse@oleane.net NNTP-Posting-Date: Mon, 7 Nov 2005 13:01:25 +0000 (UTC) User-Agent: Mozilla Thunderbird 1.0.6 (Windows/20050716) X-Accept-Language: fr, en In-Reply-To: Xref: g2news1.google.com comp.lang.ada:6265 Date: 2005-11-07T13:52:32+01:00 List-Id: Maciej Sobczak a �crit : > 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; > > > 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. Providing a default value for the discriminant is the way to achieve this. Why this rule? Here is how it goes: 1) You want to declare a "mutable" (unconstrained) object: A : Discriminated; 2) A discriminant is too important to be left uninitialized 3) Therefore, declaring unconstrained objects is allowed only if the discriminants have default values. QED. > 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. > Oh, I see your confusion here. You are not required to initialize your variable even if you don't provide default values for the discriminants, you just need to state the constraint: A : Discriminated (First); In the form you use, the constraint comes from the initial value which makes it "look" unconstrained. BTW, such a declaration was not allowed (for variables) in Ada83. You can force initialization of variables by giving it unknown discriminants, but the type has to be private. If you don't mind a run-time rather than compile-time check, you can do the following: function Raise_PE return integer is begin raise Program_Error; return 1; -- Required by the compiler end; type Must_Be_Initialized is record Junk : integer := Raise_PE; ... end record; -- --------------------------------------------------------- J-P. Rosen (rosen@adalog.fr) Visit Adalog's web site at http://www.adalog.fr