comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: Q: Primitive operation of a type
Date: 1997/07/01
Date: 1997-07-01T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023680000107972317060001@news.ni.net> (raw)
In-Reply-To: 5oq51h$t7u@netline.jpl.nasa.gov


In article <5oq51h$t7u@netline.jpl.nasa.gov>, vsnyder@math.jpl.nasa.gov
(Van Snyder) wrote:

>I've looked at Barnes's book, and Cohen's book, and I can't
>decide whether it's possible for a procedure to be a primitive
>operation of more than one type.

It is, but an operation can only be primitive for one tagged type.  

For example, the operation Set_Col in package Ada.Text_IO is primitive for
both type File_Type and type Positive_Count.  But note that neither of
these types are tagged.

These issues are discussed in Mats Weber's PhD thesis.

>If it's not possible, please send me a reference to the
>relevant parts of the LRM. [snip]

No, it is not possible for an operation to be primitive for more than one
tagged type.

>Which procedure is used when P is invoked with arguments of
>types T11 and T21?  Please refer me to sections of the LRM (or
>Barnes's or Cohen's books) that explain the answer.

Be careful though, to not interpret "can't be primitive for more than one
type" to mean "can't have different tagged types as subprogram parameters." 
A operation can be primitive for one tagged type, but take additional
class-wide parameters of another type.  For example,

package P is

   type T1 is tagged private;

   type T2 is tagged private;

   procedure Op1 (O1 : T1; O2 : T2);  -- illegal

   procedure Op2 (O1 : T1; O2 : T2'Class);  -- AOK

   procedure Op3 (O1 : T1'Class; O2 : T2);  -- AOK2

   procedure Op4 (O : T1'Class);

   procedure Op5 (O : T2'Class);
 
   procedure Op6 (O1 : T1'Class; O2 : T2'Class);

   procedure Op7 (O1 : T1; O2 : T1);  -- OK
...
end P;

Op1 is illegal, because it takes more than one kind of tagged type as
parameters.

Op2 is a primitive operation of type T1, and it gets inherited by types
that derive from T1.

Op3 is a primitive opeation of type T2, and it gets inherited by types that
derive from T2.

Op2, because it takes an object of type T2'Class, is not a primitive
operation of type T2.

Op3, because it takes an object of type T1'Class, is not a primitive
operation of type T3.

Op4 is not a primitive operation of any type.  Ditto for Op5 and Op6.

Op7 is a primitive operation of type T1, and gets inherited by types that
derive from T1.  It's OK to have more than one parameter of a tagged type,
it's just that they have to have the same tag.

Op7 is interesting, because sometimes a run-time check will need to be made
to ensure that both objects really have the same tag.  For example,

procedure P (O1, O2 : T1'Class) is
begin
   Op7 (O1, O2);
end;

This will dispatch on the tag of the O1 and O2, but only if O1 and O2 have
the same tag, which can't be determined until run-time.  If the tag-check
fails, then an exception is raised (I forget which, either Constraint_Error
or Program_Error).

One exception to this must-be-same-tag rule is in the case of the equality
operator:

procedure P (O1, O2 : T1'Class) is
begin
   if O1 = O2 then

This will dispatch on the equality operator, if the tags are the same.  If
the tags are different, then obviously the objects can't be the same, so no
exception is raised (thankfully) and the function just returns False.

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




  reply	other threads:[~1997-07-01  0:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-06-25  0:00 Q: Primitive operation of a type Van Snyder
1997-07-01  0:00 ` Matthew Heaney [this message]
1997-07-02  0:00   ` Mats Weber
1997-07-03  0:00     ` Matthew Heaney
1997-07-08  0:00       ` Mats Weber
1997-07-14  0:00         ` Matthew Heaney
1997-07-02  0:00 ` Martin C. Carlisle
replies disabled

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