comp.lang.ada
 help / color / mirror / Atom feed
From: anon@anon.org (anon)
Subject: Re: Extending discriminant types
Date: Thu, 20 Nov 2008 07:21:42 GMT
Date: 2008-11-20T07:21:42+00:00	[thread overview]
Message-ID: <ae8Vk.143630$Mh5.83121@bgtnsc04-news.ops.worldnet.att.net> (raw)
In-Reply-To: oiRUk.141685$Mh5.126924@bgtnsc04-news.ops.worldnet.att.net

Since, the orginal post suggest that the poster was using GNAT Ada 95 
compiler. Because the poster states that he can compiler the code. So, 
I used Gnat 3.15p using Ada 95 specs.

Plus, the RM does not limited the "Base" attribute to only Scalar Types. 
Any defined type can use the "Base" attribute.

Then you also have RM 2005 3.8.1 (17).

--
-- This specification was found on Ada-auth.org web site.
--
package U is
  type T is tagged private ;

  function "=" ( Left, Right : T'Base ) return Boolean ;

private
  type T is tagged null record ;
end U ;

--
-- Dummy body package to comply with the specification package.
--
package body U is

  function "=" ( Left, Right : T'Base ) return Boolean is
    begin
      return False ;
    end ;
end U ;

the previous code is valid in Ada 95 but will not compile using GNAT 
Ada 2005 (2007 or 2008 versions). 


And since a few here, like the "wiki" web sites:

From:        http://en.wikibooks.org/wiki/Ada_Programming/Types

Under the section "Defining new types and subtypes". The page defines that 
for all T, "there is also a base type called T'Base, which encompasses all 
values of T."

And under section "Base type" it says "In Ada, all types are anonymous and 
only subtypes may be named. There is a special subtype of the anonymous 
type, called the base type, which is nameable with the 'Base attribute. The 
base type comprises all values of the first subtypes."

That states that the "Base" attribute not only included in all Scalar Types 
but all defined types as well. Which means that the Non Scalar defined 
"Access" type has a special subtype aka "Base" attribute that can be used.

Plus, the concept of using the 'Base attribute and the 'Class attribute are 
the a part of the core of "Object-oriented" programming in Ada 95. With 
the "with record" part indicates the extension to the base type. But without 
this design it defect the purpose of some "Object-oriented" programming. 
There must be a base type for all types as well as all objects. 

So, either the definition of the "Base" attribute has been altered or 
limited by GNAT in the Ada 2005 specification. Or the GNAT Ada Pro 6.01 
and GNAT Ada 2007 and 2008 now contains an error for the "Base" attribute. 


Now, back to the example! The type "Child" is defined in the following 
statement:

   type Child is new KBase (B) with null record;

And the "Child" type is constrained by the record "KBase (B)" but the 
"Base" type or unconstrained subtype of type "Child" is the complete 
range of values, that is, a set comprising of a KBase(A) record and 
a KBase(B) record.


So the statement 

   Var1 : Base_Access := new Child'Base(K => B);

used with the statement

   type Base_Access is access all KBase'Class;

is legal because the statement generates an access value that has a 
Base type ("Base" attribute) value that is within the range of 
values of all Base_Access values. A variant define as "K => A" is 
illegal only during runtime when the value is assigned to the Var1 
variable which is limited to a Base_Access that has a "KBase (B)" 
define by the type "Child".



In <oiRUk.141685$Mh5.126924@bgtnsc04-news.ops.worldnet.att.net>, anon@anon.org (anon) writes:
>
>--  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 <bellon@software-erosion.org> 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
>>
>




  parent reply	other threads:[~2008-11-20  7:21 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-11-15  9:16 Extending discriminant types Stefan Bellon
2008-11-15 18:50 ` Jeffrey R. Carter
2008-11-17 16:30 ` Adam Beneschan
2008-11-18 11:02   ` christoph.grein
2008-11-18 23:24     ` Adam Beneschan
2008-11-19  9:49 ` anon
2008-11-19 10:15   ` christoph.grein
2008-11-19 19:19     ` Georg Bauhaus
2008-11-19 11:38   ` Martin
2008-11-20  7:21   ` anon [this message]
2008-11-20  8:30     ` christoph.grein
2008-11-20  8:36       ` Ludovic Brenta
2008-11-20 11:45         ` Georg Bauhaus
2008-11-20 11:46           ` Georg Bauhaus
2008-11-20 23:01             ` anon
2008-11-21 11:54               ` Ludovic Brenta
2008-11-20 14:03           ` Dmitry A. Kazakov
2008-11-20 15:03           ` Robert A Duff
2008-11-20 15:57             ` Stefan Bellon
2008-11-21  0:32               ` Adam Beneschan
2008-11-20 16:11           ` Adam Beneschan
2008-11-20 22:59       ` anon
2008-11-21  0:29         ` Adam Beneschan
2008-11-21  7:25           ` anon
2008-11-21  9:19             ` Jean-Pierre Rosen
2008-11-21 10:11             ` christoph.grein
2008-11-21 12:00             ` Ludovic Brenta
2008-11-21 22:31               ` anon
2008-11-22  0:18                 ` Stefan Bellon
2008-11-23  4:06                   ` anon
2008-11-23  5:39                     ` Georg Bauhaus
2008-11-23  8:00                       ` anon
2008-11-24 10:08                         ` Georg Bauhaus
2008-11-24 22:16                           ` anon
2008-11-25 10:37                             ` Martin
2008-11-25 11:24                               ` Georg Bauhaus
2008-11-25 20:49                                 ` Jeffrey R. Carter
2008-11-25 21:01                                   ` Adam Beneschan
2008-11-25 22:09                                     ` Georg Bauhaus
2008-11-25 22:33                                       ` Jeffrey R. Carter
2008-11-26  0:58                                         ` Adam Beneschan
2008-11-26  1:45                                           ` Jeffrey R. Carter
2008-11-26  9:31                                             ` Martin
2008-11-26  9:38                                               ` Stefan Bellon
2008-11-26  9:48                                                 ` Martin
2008-11-26 10:16                                                   ` Stefan Bellon
2008-11-26 11:05                                                     ` Ludovic Brenta
2008-11-26 11:33                                                       ` Stefan Bellon
2008-11-26 15:49                                                       ` Adam Beneschan
2008-11-26 16:15                                                         ` Ludovic Brenta
2008-11-26 11:10                                                 ` Niklas Holsti
2008-11-23  8:48                     ` Compiler quality (was: Extending discriminant types) Ludovic Brenta
2008-11-24 23:33                       ` anon
2008-11-25  6:54                         ` christoph.grein
2008-11-25 10:01                         ` Ludovic Brenta
2008-11-26 23:34                           ` anon
2008-11-27 10:24                             ` Compiler quality Georg Bauhaus
2008-11-27 14:46                               ` Ludovic Brenta
2008-11-28  9:13                                 ` Martin
2008-11-28 10:28                                   ` Georg Bauhaus
2008-12-02  3:51                                     ` Randy Brukardt
2008-11-22 13:10                 ` Extending discriminant types Gautier
2008-11-24  8:24                 ` christoph.grein
2008-11-21  6:11         ` christoph.grein
2008-11-21 21:12           ` Jeffrey R. Carter
2008-11-22 16:41             ` sjw
2008-11-21 11:44         ` Martin
2008-11-20  9:55     ` Martin
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox