comp.lang.ada
 help / color / mirror / Atom feed
* Question about tagged types and functions
@ 1996-05-21  0:00 David Hunter
  1996-05-22  0:00 ` Laurent Guerby
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: David Hunter @ 1996-05-21  0:00 UTC (permalink / raw)



Given:

  package Test is

    type BaseType is tagged record
      null;
    end BaseType;

    function Create return BaseType;

    type DerivedType is new BaseType with record
      null;
    end DerivedType;

  end Test;

Why does Ada-95 (GNAT 3.01a) tell me that I also
*must* have:

    function Create return DerivedType;

There is something going on with tagged types
and functions that I don't understand because
if I use

    procedure Create(a : out BaseType);

it (Ada-95) does not object.

===============================================
David Hunter (hunter@mwc.edu)
CPSC Department
Mary Washington College
Fredericksburg, VA 22401





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

* Re: Question about tagged types and functions
  1996-05-21  0:00 Question about tagged types and functions David Hunter
@ 1996-05-22  0:00 ` Laurent Guerby
  1996-05-22  0:00   ` David Hunter
  1996-05-22  0:00 ` Jon S Anthony
  1996-05-23  0:00 ` Jon S Anthony
  2 siblings, 1 reply; 5+ messages in thread
From: Laurent Guerby @ 1996-05-22  0:00 UTC (permalink / raw)



David> Why does Ada-95 (GNAT 3.01a) tell me that I also *must* have:

David>     function Create return DerivedType;

David> There is something going on with tagged types and functions
David> that I don't understand because if I use

David>     procedure Create(a : out BaseType);

David> it (Ada-95) does not object.

   The status of functions (primitives) that dispatch on result is
special in Ada 95. See RM95-3.9.3(4-6). Or better, read the paper "Ada
9X Tagged Types and their Implementation in GNAT" by Cyrille Comar and
Brett Porter (may be available from http://www.gnat.com, if not in the
TriAda94 Conference proceedings). There's a section devoted to this
error message (4.4) (among other OO error messages ;-).

-- 
--  Laurent Guerby, student at Telecom Bretagne (France), Team Ada.
--  "Use the Source, Luke. The Source will be with you, always (GPL)."
--  http://www-eleves.enst-bretagne.fr/~guerby/ (GATO Project).
--  Try GNAT, the GNU Ada 95 compiler (ftp://cs.nyu.edu/pub/gnat).




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

* Re: Question about tagged types and functions
  1996-05-22  0:00 ` Laurent Guerby
@ 1996-05-22  0:00   ` David Hunter
  0 siblings, 0 replies; 5+ messages in thread
From: David Hunter @ 1996-05-22  0:00 UTC (permalink / raw)



In article <4xpw7xpoc6.fsf@leibniz.enst-bretagne.fr>, 
Laurent.Guerby@enst-bretagne.fr says...
>
>David> Why does Ada-95 (GNAT 3.01a) tell me that I also *must* have:
>
>David>     function Create return DerivedType;
>
>David> There is something going on with tagged types and functions
>David> that I don't understand because if I use
>
>David>     procedure Create(a : out BaseType);
>
>David> it (Ada-95) does not object.
>
>   The status of functions (primitives) that dispatch on result is
>special in Ada 95. See RM95-3.9.3(4-6). Or better, read the paper "Ada
>9X Tagged Types and their Implementation in GNAT" by Cyrille Comar and
>Brett Porter

Thank you for your timely response.

I read this paper and there is indeed a paragraph that explictly
addresses my question.  They explain that the overloading of Create is
necessary because: "otherwise the inherited function would return
an object of the parent type, that is to say an incomplete
object."  Obviously I can see that you want to avoid any
situation in which you end up with "derived_object := parent_object"
but doesn't Ada-95 forbid that anyway?  I mean, you couldn't do

	d : DerivedType;
	b : BaseType;
	d := b;

so the error "d := Create;" would be caught by the type
rules anyway, right?  I guess I'm back to my original
question.  Maybe it would help if someone could provide
an example in which the problem described by the
paper occurs (one that would not be otherwise detected).

==============================
David Hunter (hunter@mwc.edu)
CPSC Dept.
Mary Washington College
Fredericksburg, VA 22401






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

* Re: Question about tagged types and functions
  1996-05-21  0:00 Question about tagged types and functions David Hunter
  1996-05-22  0:00 ` Laurent Guerby
@ 1996-05-22  0:00 ` Jon S Anthony
  1996-05-23  0:00 ` Jon S Anthony
  2 siblings, 0 replies; 5+ messages in thread
From: Jon S Anthony @ 1996-05-22  0:00 UTC (permalink / raw)



In article <4ntg05$qgc@ns.interserf.net> dhunter@interserf.net (David Hunter) writes:

>   package Test is
> 
>     type BaseType is tagged record
>       null;
>     end BaseType;
> 
>     function Create return BaseType;
> 
>     type DerivedType is new BaseType with record
>       null;
>     end DerivedType;
> 
>   end Test;
> 
> Why does Ada-95 (GNAT 3.01a) tell me that I also
> *must* have:
> 
>     function Create return DerivedType;
> 
> There is something going on with tagged types
> and functions that I don't understand because
> if I use
> 
>     procedure Create(a : out BaseType);
> 
> it (Ada-95) does not object.

The short answer is that the would be non-overridden inherited Create
would not provide values for any extension components in the derived
type and so the value it would return would be ill defined.  Now, in
your case, there are no extension components, but special casing for
this situation is not a particularly good idea for several reasons.

Admittedly, this is virtually impossible to find in the ARM95 if you
go looking for it.  Why?  Because it is stuck away in 3.9.3, the major
topic of which is _abstract_ types and subprograms.  In particular,
see paragraphs (4-6).  This really should have been mentioned (perhaps
in addition to the 3.9.3 comments) in 3.4.  Say between (22) and (23).


/Jon
-- 
Jon Anthony
Organon Motives, Inc.
1 Williston Road, Suite 4
Belmont, MA 02178

617.484.3383
jsa@organon.com





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

* Re: Question about tagged types and functions
  1996-05-21  0:00 Question about tagged types and functions David Hunter
  1996-05-22  0:00 ` Laurent Guerby
  1996-05-22  0:00 ` Jon S Anthony
@ 1996-05-23  0:00 ` Jon S Anthony
  2 siblings, 0 replies; 5+ messages in thread
From: Jon S Anthony @ 1996-05-23  0:00 UTC (permalink / raw)



In article <4nvhdo$ri7@ns.interserf.net> dhunter@interserf.net (David Hunter) writes:

> situation in which you end up with "derived_object := parent_object"
> but doesn't Ada-95 forbid that anyway?  I mean, you couldn't do
> 
> 	d : DerivedType;
> 	b : BaseType;
> 	d := b;
> 
> so the error "d := Create;" would be caught by the type
> rules anyway, right?

This is pretty much why the error is being flagged the way it is.  If
you assume that the current rule was not there and you did not have to
override, then a new Create with result type DerivedType is implicitly
declared _immediately_ after the declaration of DerivedType.  So, the
case you site:

    d := Create;

would not have any type error at all.  But, the actual Create bound
here would be the Create with result type BaseType with an implicit
conversion on the resulting object _away_ from the root of BaseType
to DerivedType.  Ba-Boom!  See 3.4(17-23, 27) and 3.9.2(20).

/Jon

-- 
Jon Anthony
Organon Motives, Inc.
1 Williston Road, Suite 4
Belmont, MA 02178

617.484.3383
jsa@organon.com





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

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

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-05-21  0:00 Question about tagged types and functions David Hunter
1996-05-22  0:00 ` Laurent Guerby
1996-05-22  0:00   ` David Hunter
1996-05-22  0:00 ` Jon S Anthony
1996-05-23  0:00 ` Jon S Anthony

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