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-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!feeder.news-service.com!newsfeed.straub-nv.de!uucp.gnuu.de!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail Date: Wed, 02 Dec 2009 01:32:23 +0100 From: Georg Bauhaus User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.4pre) Gecko/20090915 Thunderbird/3.0b4 MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Operation can be dispatching in only one type References: <025105f2-5571-400e-a66f-ef1c3dc9ef32@g27g2000yqn.googlegroups.com> <1w0q3zxzw79pt$.5z0juiky7kfd$.dlg@40tude.net> <0f177771-381e-493b-92bb-28419dfbe4e6@k19g2000yqc.googlegroups.com> <1nbcfi99y0fkg.1h5ox2lj73okx$.dlg@40tude.net> <59acf311-3a4a-4eda-95a3-22272842305e@m16g2000yqc.googlegroups.com> <4b150869$0$6732$9b4e6d93@newsspool2.arcor-online.net> <18vlg095bomhd.8bp1o9yysctg$.dlg@40tude.net> <4b152ffe$0$7615$9b4e6d93@newsspool1.arcor-online.net> <19nhib6rmun1x$.13vgcbhlh0og9$.dlg@40tude.net> <4b1557d0$0$7623$9b4e6d93@newsspool1.arcor-online.net> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <4b15b59b$0$7632$9b4e6d93@newsspool1.arcor-online.net> Organization: Arcor NNTP-Posting-Date: 02 Dec 2009 01:32:27 CET NNTP-Posting-Host: 3e3da194.newsspool1.arcor-online.net X-Trace: DXC=W\1Rle^2:6dOKO]LCQ@0g`ic==]BZ:afn4Fo<]lROoRa<`=YMgDjhgbV\;T7b2ZF?hPCY\c7>ejVhB08Yed0=a>a]ebhO<2IU?h X-Complaints-To: usenet-abuse@arcor.de Xref: g2news1.google.com comp.lang.ada:8278 Date: 2009-12-02T01:32:27+01:00 List-Id: On 12/1/09 10:53 PM, John B. Matthews wrote: > If I may amplify on this, the Java compiler rejects the assignment to > its_color because it's a local variable, which must have an explicit > value before use [1]. Once Tire's constructor completes, the value of > spare.rim_color has the default value null [1]. The compiler need only > check that its_color has been assigned in the current scope [2]. In > Georg Bauhaus' example, the constructor is invoked in a nested scope. > > In Ada, I get a warning that '"Spare" is read but never assigned.' > > type Tire_Color is (Black, White); > type Tire is record > Rim_Color : Tire_Color; > end record; > Spare : Tire; > > If I throw in "for Tire_Color use (Black => 1, White => 2)," the > implicit initial value [3] of Rim_Color is not valid for Tire_Color. > It's "a bounded error to evaluate the value of such an object [4]," and > I get CONSTRAINT_ERROR at run-time. > > The Java compiler doesn't warn that spare.rim_color is null by default; > the Ada compiler doesn't warn that Spare.Rim_Color is invalid by > default. In either language, I have to either accept the default initial > values or specify them. I sense I'm missing something. The Java rule I had been thinking of starts from less emphasis on what the default initial (Ada) value of a (local) variable might be, given current Ada rules. Rather, in the sequence of statements below the compiler would just not accept the reference to the .Rim_Color component of Spare. The meaning of the declaration Spare : Tire needs to be understood as slightly changed, to exclude (or ignore) default initialization. Spare : Tire; begin -- Here, whatever Spare is, or whichever side effects its -- declaration may have, it is not used between -- its declaration and the line following the if statement. -- Therefore, we are free to think of it as something -- or as nothing, or as something to become a Tire when -- necessary. A virtual object, perhaps. (Otherwise, use -- syntax to indicate that there is something important -- going on in default init; or, for compatibility, when -- nothing important is going on.) if Some_Condition then Spare := Make_a_Tire; end if; Its_Color := Spare.Rim_Color; -- illegal A simple rule would now be a copy of the Java rule which is quoted below. Just assume that Spare has no value. Just like the last line is not accepted by SPARK or by Java (the corresponding Java source line). The warning which some Ada compilers will issue (that Spare may not have been assigned a value) is then turned into an error. As might be expected in Ada, some syntax might be in order to say that default initialization does provide an initial value that warrants the safe use of the variable after the if statement (or is needed for its side effects, but this is another story, I guess). Another case is when a declared variable is used in a declaration of another variable following it, Spare : Tire; Another : Tire := Spare; -- might become illegal begin ... Illegal unless it is specified that Spare does have a valid Tire value. Or, oddly, that Another "inherits" the unknown state of Spare WRT being initialized or not. This is the Java rule I had in mind. I found it thanks to the link you have supplied: "A Java compiler must carry out a specific conservative flow analysis to make sure that, for every access of a local variable or blank final field f, f is definitely assigned before the access; otherwise a compile-time error must occur." > [2] 25979>