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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,9b7d3a51d0d8b6ee X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news1.google.com!news.glorb.com!wn11feed!worldnet.att.net!bgtnsc04-news.ops.worldnet.att.net.POSTED!53ab2750!not-for-mail Newsgroups: comp.lang.ada From: anon@anon.org (anon) Subject: Re: Extending discriminant types Reply-To: no to spamers (No@email.given.org) References: <20081115101632.5f98c596@cube.tz.axivion.com> X-Newsreader: IBM NewsReader/2 2.0 Message-ID: Date: Wed, 19 Nov 2008 09:49:08 GMT NNTP-Posting-Host: 12.64.90.97 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc04-news.ops.worldnet.att.net 1227088148 12.64.90.97 (Wed, 19 Nov 2008 09:49:08 GMT) NNTP-Posting-Date: Wed, 19 Nov 2008 09:49:08 GMT Organization: AT&T Worldnet Xref: g2news2.google.com comp.lang.ada:3669 Date: 2008-11-19T09:49:08+00:00 List-Id: -- Your talking about "Variant Parts and Discrete Choices" (RM 3.8.1) -- which is validated during runtine, if the variable is reference. -- -- Now with a smart compiler it could detect the error, but the -- compiler in GNAT is not that smart. procedure t is -- Change "Base" to "KBase" to prevent confusion with predefine -- Attribute 'Base, which als works. type Kind is (A, B); type KBase (K : Kind) is abstract tagged record case K is when A => null; when B => Dummy : Natural; end case; end record; type Base_Access is access all KBase'Class; -- type "Child" is bound to KBase ( K => B ) so any reference -- of "Child" to ( K => A ) should cause an error condition -- but this only happens during runtime. type Child is new KBase (B) with null record; -- Var2 is in error and should cause a Constaint_Error -- once referenced. Var : Base_Access := new Child ; Var0 : Base_Access := new Child'(K => B, Dummy => 5); Var1 : Base_Access := new Child'Base(K => B); -- Just for Adam, this works Var2 : Base_Access := new Child'Base(K => A); begin -- t Var.Dummy := 2 ; Var0.Dummy := 4 ; Var1.Dummy := 8 ; -- Just for Adam, this works -- Uncommenting the following line will result in a Constraint_Error -- during runtine. But until Var2 is reference the validation of -- type is not performed. -- Var2.Dummy := 16 ; end t ; In <20081115101632.5f98c596@cube.tz.axivion.com>, Stefan Bellon writes: >Hi, > >I stumbled across some behaviour when extending an unconstrained >discriminant type while providing the constraint when extending. Assume >the following code: > > type Kind is (A, B); > > type Base (K : Kind) is abstract tagged record > case K is > when A => null; > when B => Dummy : Natural; > end case; > end record; > > type Base_Access is access all Base'Class; > > type Child is new Base (B) with null record; > >In this case I would have thought that type Child is now constrained to >Kind B and no object of type Child with Kind A can be created. > >However, the following compiles (with GNAT) and does not throw an >exception at runtime: > > Var : Base_Access := new Child'(K => A); > >Am I misunderstanding the Ada semantics? If so, is there a way to >achieve what I'm trying, i.e. that I can determine the Kind while >extending and not change it when creating objects? > >Greetings, >Stefan >