comp.lang.ada
 help / color / mirror / Atom feed
* Re: argument of conversion cannot be an allocator
  1997-05-14  0:00 argument of conversion cannot be an allocator Dale Stanbrough
@ 1997-05-14  0:00 ` Peter Hermann
  1997-05-16  0:00 ` Dale Stanbrough
  1997-05-17  0:00 ` John G. Volan
  2 siblings, 0 replies; 5+ messages in thread
From: Peter Hermann @ 1997-05-14  0:00 UTC (permalink / raw)



Dale Stanbrough (dale@goanna.cs.rmit.EDU.AU) wrote:
: I came across the following problem...

: 	type a is tagged private;
: 	type a_ptr is access all a'class;
: 	
: 	type b is new a with private;
: 	type b_ptr is acces all b'class;
: 	
: 	...
: 	
: 	return a'(new b);

This compiled:

PROCEDURE Dale IS

   type a is tagged null record;
   type a_ptr is access all a'class;

   type b is new a with
      record
         bcomp : integer := 0;
      end record;

   function fu return a_ptr is
   begin
      return new b; -- (i.e. newbee)
   end fu;

BEGIN
                 null;
END Dale ;

or else I possibly misunderstood you ?

--
Peter Hermann  Tel:+49-711-685-3611 Fax:3758 ph@csv.ica.uni-stuttgart.de
Pfaffenwaldring 27, 70569 Stuttgart Uni Computeranwendungen
Team Ada: "C'mon people let the world begin" (Paul McCartney)




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

* argument of conversion cannot be an allocator
@ 1997-05-14  0:00 Dale Stanbrough
  1997-05-14  0:00 ` Peter Hermann
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Dale Stanbrough @ 1997-05-14  0:00 UTC (permalink / raw)



I came across the following problem...


	type a is tagged private;
	type a_ptr is access all a'class;
	
	type b is new a with private;
	type b_ptr is acces all b'class;
	
	...
	
	
	return a'(new b);

which results in the error message (from GNAT)

	"argument of conversion cannot be an allocator"

The work around is...

	declare
	   temp : b_Ptr := new b;
	begin
	   return a_ptr(temp);
	end;


I presume there is a good reason for this; can anyone provide enlightenment
on this point (or even a simpler way to accomplish the above)?

Dale




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

* Re: argument of conversion cannot be an allocator
  1997-05-16  0:00 ` Dale Stanbrough
@ 1997-05-16  0:00   ` Robert Dewar
  0 siblings, 0 replies; 5+ messages in thread
From: Robert Dewar @ 1997-05-16  0:00 UTC (permalink / raw)



Dale asks

<<Ah, so are you saying the the type conversion (a_ptr) only "looks at"
the "new", and can't deduce (or doesn't have to) the resultant type from
the "new b"?>>

Not quite, obviously you cannot tell the type of "new x
" by looking at it in isolation (it could be any of a number of different
access types). So you can only use "new x" in contexts where you can figure
out the required type using normal overloading rules, in a way that gives
a unique answer.

But in a type conversion, you only know what type you are going TO, not
what you are coming from, so the type you are coming from must be
uniquely determined by the expression being converted.

I often find that people get type conversions and type qualiffication
badly mixed up, and use the former when they want the latter. I think
that is what happened to you in this case. 

For example, suppose we have two functions

   function f return integer;
   function f return real;

then

    integer(f)

is clearly illegal, since it is ambiguous, but
integer'(f) is fine, and is the proper way of making sure you
get the version of f you want.

type conversions of pointers are unusual
type qualifications of allocators are often required





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

* Re: argument of conversion cannot be an allocator
  1997-05-14  0:00 argument of conversion cannot be an allocator Dale Stanbrough
  1997-05-14  0:00 ` Peter Hermann
@ 1997-05-16  0:00 ` Dale Stanbrough
  1997-05-16  0:00   ` Robert Dewar
  1997-05-17  0:00 ` John G. Volan
  2 siblings, 1 reply; 5+ messages in thread
From: Dale Stanbrough @ 1997-05-16  0:00 UTC (permalink / raw)



Robert Dewar writes:

"The reason is obvious, a conversion provides no context for its operand,
 so you have to be able to tell what the type of the operand is by looking
 at it, but an allocator is no good, since it only knows its type from
 context. The fix is far simpler than what you have:
 
     a_ptr (b_ptr'(new b))
 
 although I am a little surprised that just plain a_ptr'(new b) does not
 work, why do you need a conversion here at all?"

Ah, so are you saying the the type conversion (a_ptr) only "looks at"
the "new", and can't deduce (or doesn't have to) the resultant type from
the "new b"?

I've since found out that i don't need the conversion at all. The function
i was writing was defined as returning a pointer (or access type :-)
to a parent type, while the body was returning pointers to derived types.
I figured you should be able to do this, but didn't realise there would
be automatic type conversion b/w b_ptr and a_ptr (even though this is
quite safe) (or is it a view conversion?).

Dale




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

* Re: argument of conversion cannot be an allocator
  1997-05-14  0:00 argument of conversion cannot be an allocator Dale Stanbrough
  1997-05-14  0:00 ` Peter Hermann
  1997-05-16  0:00 ` Dale Stanbrough
@ 1997-05-17  0:00 ` John G. Volan
  2 siblings, 0 replies; 5+ messages in thread
From: John G. Volan @ 1997-05-17  0:00 UTC (permalink / raw)



Dale Stanbrough wrote:
> 
> I came across the following problem...
> 
>         type a is tagged private;
>         type a_ptr is access all a'class;
> 
>         type b is new a with private;
>         type b_ptr is acces all b'class;
> 
>         ...
> 
> 
>         return a'(new b);

I assume you meant:

          return a_ptr(new b);
> 
> which results in the error message (from GNAT)
> 
>         "argument of conversion cannot be an allocator"

Just so everyone's clear on this, if you're trying to generate an a_ptr,
there is no need to do a type conversion at all:

   Your_A_Ptr : A_Ptr := new B;

This is perfectly legal, and it makes perfect sense, too. An A_Ptr
points at any object in type A'Class, which is a classwide type that
covers all objects of type A as well as all types derived from A.  This
includes type B.

------------------------------------------------------------------------
Internet.Usenet.Put_Signature 
  (Name => "John G. Volan",  Home_Email => "johnvolan@sprintmail.com",
   Slogan => "Ada95: The World's *FIRST* International-Standard OOPL",
   Disclaimer => "These opinions were never defined, so using them " & 
     "would be erroneous...or is that just nondeterministic now? :-) ");
------------------------------------------------------------------------




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

end of thread, other threads:[~1997-05-17  0:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-05-14  0:00 argument of conversion cannot be an allocator Dale Stanbrough
1997-05-14  0:00 ` Peter Hermann
1997-05-16  0:00 ` Dale Stanbrough
1997-05-16  0:00   ` Robert Dewar
1997-05-17  0:00 ` John G. Volan

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