comp.lang.ada
 help / color / mirror / Atom feed
From: "Steven Deller" <deller@smsail.com>
Subject: RE: what means the " ' " in use with a record type?
Date: Sun, 25 Aug 2002 18:29:04 -0500
Date: 2002-08-25T18:29:04-05:00	[thread overview]
Message-ID: <mailman.1030318262.1178.comp.lang.ada@ada.eu.org> (raw)
In-Reply-To: <H1Ep8v.9v2@world.std.com>

 >   If x is an access to type node, why is the qualified expression 
> > needed, while it's not needed in:
> >   type fruits is (apple, orange, banana);
> >   type colors is (red, orange, yellow);
> >   color : colors;
> >   fruit : fruits;
> > begin
> >   color := orange;
> >   fruit := orange;

T(x) is a type conversion (cast :-)) of x to type T.
The actual type of "x" need only be compatible with T, not "of" type T. 
 
T'(x) on the other hand is an "x" of type T.

Qualification is needed in many cases having nothing to do with
allocators.
See 4.7 for examples of some.

> As I recall, the original reason for the apostrophe is 
> because, in the absence of this feature, the general construct
>   new T(expr)
> could be ambiguous (or at least confusing to the human 
> reader) in some contexts -- i.e., is expr the value of a 
> discriminant, or is it an initialization for the allocated 
> object?  

No.  It means something different with and without the apostrophe.

If it were legal Ada to write:
   type A is access Node ;
   P : A ;
   ...
   P := new Node(x) ;
   ... or simply
   P := new (x) ;

then the type of x could be anything compatible with Node.  In
particular,
if Node were derived from N without any extensions, then x's value could
be of either Node or N.

That may seem trivial, until you consider type attributes, where N might
be allocated from shared memory (using a user-specified allocator), 
while Node is in default (non-shared) memory.

You could argue that Ada just ought to "do the simple/right thing", but
that is a slippery slope to C-style disasters. Better that the 
language force the writer to be specific about the type intended. 

Plus there is no "right" thing when you have tagged types with
extensions:
  procedure Test_Ada is
    type A is tagged record
        X : integer ;
    end record ;
    type B is tagged record
        Y : integer ;
    end record ;
    type C is tagged record
        Z : integer ;
    end record ;
    type P is access A'class ;
    O : P ;
  begin
    O := new B'(X => 3, others => 4 ) ; -- "B" or "C" is legal here
    ...                               ; Ada makes you say what you mean

Regards,
Steve Deller (the "other" SteveD :-))
deller@smsail.com




  reply	other threads:[~2002-08-25 23:29 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <c923f575.0208231040.301add2@posting.google.com>
2002-08-23 20:16 ` what means the " ' " in use with a record type? tmoran
2002-08-24 13:19   ` Robert Dewar
2002-08-25  6:15     ` tmoran
2002-08-25 15:42       ` Ben Brosgol
2002-08-25 23:29         ` Steven Deller [this message]
2002-08-26  1:20           ` tmoran
2002-08-26 20:43             ` Robert Dewar
2002-08-26 20:41       ` Robert Dewar
2002-08-26 21:37         ` tmoran
2002-08-25  5:00 ` R. Tim Coslet
replies disabled

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