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,dad94612ff745427 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!wns13feed!worldnet.att.net!attbi_s72.POSTED!53ab2750!not-for-mail From: "Jeffrey R. Carter" Organization: jrcarter at acm dot org User-Agent: Thunderbird 1.5 (Windows/20051201) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Instantiating private types with discriminants? References: <1147252198.138173.203910@j73g2000cwa.googlegroups.com> <44623869$0$4504$9b4e6d93@newsread2.arcor-online.net> <87d5elk8e9.fsf@ludovic-brenta.org> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: NNTP-Posting-Host: 12.201.97.176 X-Complaints-To: abuse@mchsi.com X-Trace: attbi_s72 1147310253 12.201.97.176 (Thu, 11 May 2006 01:17:33 GMT) NNTP-Posting-Date: Thu, 11 May 2006 01:17:33 GMT Date: Thu, 11 May 2006 01:17:33 GMT Xref: g2news2.google.com comp.lang.ada:4188 Date: 2006-05-11T01:17:33+00:00 List-Id: Rick H wrote: > > But all this has a side-effect that I haven't considered: the fact > that a variable declared as a class-wide type *requires* initialization > means that the compiler can determine the variable's type statically. > This means that the following will raise a constraint error: > declare > Var_A : General_T'Class := Get (10); -- Type_A > begin > Put (Var_A); > Ada.Text_IO.New_Line; > Var_A := Get (100.0); -- Type_B - constraint error! > Put (Var_A); > end; Constraint_Error is raised at run time, so this isn't static. In the most general case, a function returning the class-wide type may return any of the types in the class based on external events, and so be completely unknown statically (no matter how good a job of static analysis the compiler does). So there must be cases when the actual type of the variable is not known until run time. In practice, most compilers are not going to bother, so this will always result in dispatching. One thing about class-wide variables is that they are constrained to the type of their initialization expression, and so can never be assigned another type in the class (hence Constraint_Error). This is why it's best to declare them as late as possible. -- Jeff Carter "My name is Jim, but most people call me ... Jim." Blazing Saddles 39