comp.lang.ada
 help / color / mirror / Atom feed
* Re: what means the " ' " in use with a record type?
       [not found] <c923f575.0208231040.301add2@posting.google.com>
@ 2002-08-23 20:16 ` tmoran
  2002-08-24 13:19   ` Robert Dewar
  2002-08-25  5:00 ` R. Tim Coslet
  1 sibling, 1 reply; 10+ messages in thread
From: tmoran @ 2002-08-23 20:16 UTC (permalink / raw)


> return new node'(value => x, to => l);
> what means the apostrophe between node and (value => ......?
  It means you want to allocate a new object of type "node" and
give it an initial value of (x,l).
  From Cohen's "Ada as a second language":
    Integer_Pointer := new Integer;
leaves Integer_Pointer pointing to a dynamically allocated variable of
type Integer with unknown contents.  The assignment statement
    Integer_Pointer := new Integer'(0);
leaves Integer_Pointer pointing to a dynamically allocated variable of
type Integer containing the value zero.



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: what means the " ' " in use with a record type?
  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
  0 siblings, 1 reply; 10+ messages in thread
From: Robert Dewar @ 2002-08-24 13:19 UTC (permalink / raw)


tmoran@acm.org wrote in message news:<hKw99.5354$jD1.434739270@newssvr13.news.prodigy.com>...
> > return new node'(value => x, to => l);

> > what means the apostrophe between node and (value => ......?

>   It means you want to allocate a new object of type "node" and
> give it an initial value of (x,l).

This is highly misleading, The questioner asked about the apostrophe, and
Tom is answering about the whole construct.

The quote has nothing whatever to do with allocation.

The expression node'(....)

is called a qualified expression. In some contexts you can simply write
the aggregate (in this case (value => x, to => 1)) and it is obvious what
you are talking about. For example

   N : node := (value => x, to => 1);

is just fine because we obviously expect a node here. But if I just write

    x := new (value => x, to => 1);

then the allocator has no idea what you are talking about, since there is
nothing to say that this is a node. The qualified expression fixes this
a'b in general says, I have something of type a with the value computed by
the expression b.

So to fix the allocator we need

   x := new node'(value => x, to => 1);

and now the allocator knows what type of thing you are trying to allocate.



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: what means the " ' " in use with a record type?
       [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-25  5:00 ` R. Tim Coslet
  1 sibling, 0 replies; 10+ messages in thread
From: R. Tim Coslet @ 2002-08-25  5:00 UTC (permalink / raw)


Robert Dewar explained this correctly and fully as a Qualified Expression.

However I would like to point out that the syntax is "vaguely" similar to
that of Attributes. Sometimes I like to think of it as a sort of
pseudo-attribute of a type that allows specification of values of that type.

I KNOW its not a real Attribute, but sometimes it helps me thinking that
way. That's all I wanted to add.

-- 
        R. Tim Coslet
        r_tim_coslet@pacbell.net

Technology, n. Domesticated natural phenomena.


> From: jonas.gasser@dataflow.ch (drmed)
> Organization: http://groups.google.com/
> Newsgroups: comp.lang.ada
> Date: 23 Aug 2002 11:40:45 -0700
> Subject: what means the " ' " in use with a record type?
> 
> hi,
> 
> following code:
> 
> type node is record value : T;
> to : list;
> end record;
> 
> function cons(x : T; l : list) return list is
> begin
> return new node'(value => x, to => l);
> end cons;
> 
> what means the apostrophe between node and (value => ......?
> Has it anything to do with pointers or only in memory writing?
> 
> Jonas Gasser




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: what means the " ' " in use with a record type?
  2002-08-24 13:19   ` Robert Dewar
@ 2002-08-25  6:15     ` tmoran
  2002-08-25 15:42       ` Ben Brosgol
  2002-08-26 20:41       ` Robert Dewar
  0 siblings, 2 replies; 10+ messages in thread
From: tmoran @ 2002-08-25  6:15 UTC (permalink / raw)


> This is highly misleading, The questioner asked about the apostrophe, and
> Tom is answering about the whole construct.
  I sincerely hope I didn't mislead the questioner.  I thought about
including a mention of the more general subject of qualified expressions,
but, given the rest of the question:
> Has it anything to do with pointers or only in memory writing?
it seemed to me better to give a simple answer to the immediate question.
  I suspect that qualified expressions are fairly rare in practice
except for an allocation with an initial value.

>    x := new (value => x, to => 1);
>
> then the allocator has no idea what you are talking about, since there is
> nothing to say that this is a node. The qualified expression fixes this
  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;



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: what means the " ' " in use with a record type?
  2002-08-25  6:15     ` tmoran
@ 2002-08-25 15:42       ` Ben Brosgol
  2002-08-25 23:29         ` Steven Deller
  2002-08-26 20:41       ` Robert Dewar
  1 sibling, 1 reply; 10+ messages in thread
From: Ben Brosgol @ 2002-08-25 15:42 UTC (permalink / raw)


Tom Moran wrote:
>   I suspect that qualified expressions are fairly rare in practice
> except for an allocation with an initial value.
>
> >    x := new (value => x, to => 1);
> >
> > then the allocator has no idea what you are talking about, since there
is
> > nothing to say that this is a node. The qualified expression fixes this
>   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;

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?  The apostrophe clearly indicates
the intent, and can also serve the purpose of a type qualification as Robert
observed.

Ben Brosgol
Ada Core Technologies






^ permalink raw reply	[flat|nested] 10+ messages in thread

* RE: what means the " ' " in use with a record type?
  2002-08-25 15:42       ` Ben Brosgol
@ 2002-08-25 23:29         ` Steven Deller
  2002-08-26  1:20           ` tmoran
  0 siblings, 1 reply; 10+ messages in thread
From: Steven Deller @ 2002-08-25 23:29 UTC (permalink / raw)


 >   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




^ permalink raw reply	[flat|nested] 10+ messages in thread

* RE: what means the " ' " in use with a record type?
  2002-08-25 23:29         ` Steven Deller
@ 2002-08-26  1:20           ` tmoran
  2002-08-26 20:43             ` Robert Dewar
  0 siblings, 1 reply; 10+ messages in thread
From: tmoran @ 2002-08-26  1:20 UTC (permalink / raw)


> if Node were derived from N without any extensions, then x's value could
> be of either Node or N.
  A qualified expression is normally only needed when there is an
ambiguity like this and you need to tell the compiler which you mean.
But in the case of allocators with initial value, you _always_ have to
use a qualified expression, regardless of whether there's an ambiguity
or not.  Why the inconsistency?  For documentation purposes?



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: what means the " ' " in use with a record type?
  2002-08-25  6:15     ` tmoran
  2002-08-25 15:42       ` Ben Brosgol
@ 2002-08-26 20:41       ` Robert Dewar
  2002-08-26 21:37         ` tmoran
  1 sibling, 1 reply; 10+ messages in thread
From: Robert Dewar @ 2002-08-26 20:41 UTC (permalink / raw)


tmoran@acm.org wrote in message news:<rC_99.324$gK6.29595828@newssvr21.news.prodigy.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;

Because you cannot look inside an aggregate to determine its type. See RM
for details, and AARM for motivational information.



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: what means the " ' " in use with a record type?
  2002-08-26  1:20           ` tmoran
@ 2002-08-26 20:43             ` Robert Dewar
  0 siblings, 0 replies; 10+ messages in thread
From: Robert Dewar @ 2002-08-26 20:43 UTC (permalink / raw)


tmoran@acm.org wrote in message news:<Qnfa9.31$O%2.4390056@newssvr14.news.prodigy.com>...
> > if Node were derived from N without any extensions, then x's value could
> > be of either Node or N.
>   A qualified expression is normally only needed when there is an
> ambiguity like this and you need to tell the compiler which you mean.
> But in the case of allocators with initial value, you _always_ have to
> use a qualified expression, regardless of whether there's an ambiguity
> or not.  Why the inconsistency?  For documentation purposes?

No, it is because it is always ambiguous, given you are not allowed to look
inside the aggregate. I suppose you could have a rule that if there is only
one composite type ..... :-)



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: what means the " ' " in use with a record type?
  2002-08-26 20:41       ` Robert Dewar
@ 2002-08-26 21:37         ` tmoran
  0 siblings, 0 replies; 10+ messages in thread
From: tmoran @ 2002-08-26 21:37 UTC (permalink / raw)


> Because you cannot look inside an aggregate to determine its type. See RM

   type fruits is (apple, orange, banana);
   type colors is (red, orange, yellow);
   color : colors;
   fruit : fruits;
   type color_ptrs is access colors;
   x : color_ptrs;
 begin
   color := orange;
   fruit := orange;
   x := new color'(red);
So you can't write
   x := new red;
because red is an aggregate??
That also doesn't explain why you can't write:
   x := new;
but you can write:
   x.all := orange;



^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2002-08-26 21:37 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [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
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

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