comp.lang.ada
 help / color / mirror / Atom feed
* Object.Operation and child package
@ 2008-06-25 17:41 pascal.malaise
  2008-06-25 18:40 ` Ludovic Brenta
  2008-06-25 18:51 ` Jeffrey R. Carter
  0 siblings, 2 replies; 5+ messages in thread
From: pascal.malaise @ 2008-06-25 17:41 UTC (permalink / raw)


Hi Ada05 experts,

Is it normal to reject compilation of Object.Operation notation when
operation is in a child package:

package Pack is
  type Typ is tagged null record;
  procedure Proc1 (Var : in Typ);
end Pack;

package Pack.Sub is
  procedure Proc2 (Var : in Typ);
end Pack.Sub;

with Pack.Sub;
procedure Proc is
  Obj : Pack.Typ;
begin
  Obj.Proc1;
  Obj.Proc2;
end Proc;

The second call does not compile (with gnat):
proc.adb:6:04: no selector "Proc2" for type "Typ" defined at pack.ads:
3



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

* Re: Object.Operation and child package
  2008-06-25 17:41 Object.Operation and child package pascal.malaise
@ 2008-06-25 18:40 ` Ludovic Brenta
  2008-06-25 19:03   ` Adam Beneschan
  2008-06-25 18:51 ` Jeffrey R. Carter
  1 sibling, 1 reply; 5+ messages in thread
From: Ludovic Brenta @ 2008-06-25 18:40 UTC (permalink / raw)


pascal.malaise@gmail.com writes:

> Hi Ada05 experts,
>
> Is it normal to reject compilation of Object.Operation notation when
> operation is in a child package:
>
> package Pack is
>   type Typ is tagged null record;
>   procedure Proc1 (Var : in Typ);
> end Pack;
>
> package Pack.Sub is
>   procedure Proc2 (Var : in Typ);
> end Pack.Sub;
>
> with Pack.Sub;
> procedure Proc is
>   Obj : Pack.Typ;
> begin
>   Obj.Proc1;
>   Obj.Proc2;
> end Proc;
>
> The second call does not compile (with gnat):
> proc.adb:6:04: no selector "Proc2" for type "Typ" defined at pack.ads:
> 3

Yes, this is normal because Proc2 is not a primitive operation of Typ
(or, in other words, it is not a method of Typ).  A primitive
operation, by definition, is declared in the same package as its type.

So, in this situation, the only way to call Proc2 is with the
traditional notation:

   Pack.Sub.Proc2 (Obj);

-- 
Ludovic Brenta.



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

* Re: Object.Operation and child package
  2008-06-25 17:41 Object.Operation and child package pascal.malaise
  2008-06-25 18:40 ` Ludovic Brenta
@ 2008-06-25 18:51 ` Jeffrey R. Carter
  1 sibling, 0 replies; 5+ messages in thread
From: Jeffrey R. Carter @ 2008-06-25 18:51 UTC (permalink / raw)


pascal.malaise@gmail.com wrote:
> 
> Is it normal to reject compilation of Object.Operation notation when
> operation is in a child package:

Yes. ARM 4.1.3:

Quote : begin

9.1/2

     * A view of a subprogram whose first formal parameter is of a tagged type 
or is an access parameter whose designated type is tagged:

9.2/2

       The prefix (after any implicit dereference) shall resolve to denote an 
object or value of a specific tagged type T or class-wide type T'Class. The 
selector_name shall resolve to denote a view of a subprogram declared 
immediately within the declarative region in which an ancestor of the type T is 
declared. The first formal parameter of the subprogram shall be of type T, or a 
class-wide type that covers T, or an access parameter designating one of these 
types. The designator of the subprogram shall not be the same as that of a 
component of the tagged type visible at the point of the selected_component. The 
selected_component denotes a view of this subprogram that omits the first formal 
parameter. This view is called a prefixed view of the subprogram, and the prefix 
of the selected_component (after any implicit dereference) is called the prefix 
of the prefixed view.

end Quote;

(In interpreting this, it's important to remember that a tagged type T in an 
ancestor of itself: ARM 3.4.1.)

The operation declared in your child package is not declared immediately within 
the declarative region in which Typ is declared, so you cannot use 
Object.Operation notation for it.

-- 
Jeff Carter
"Sons of a silly person."
Monty Python & the Holy Grail
02



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

* Re: Object.Operation and child package
  2008-06-25 18:40 ` Ludovic Brenta
@ 2008-06-25 19:03   ` Adam Beneschan
  2008-06-26  5:56     ` pascal.malaise
  0 siblings, 1 reply; 5+ messages in thread
From: Adam Beneschan @ 2008-06-25 19:03 UTC (permalink / raw)


On Jun 25, 11:40 am, Ludovic Brenta <ludo...@ludovic-brenta.org>
wrote:
> pascal.mala...@gmail.com writes:
> > Hi Ada05 experts,
>
> > Is it normal to reject compilation of Object.Operation notation when
> > operation is in a child package:
>
> > package Pack is
> >   type Typ is tagged null record;
> >   procedure Proc1 (Var : in Typ);
> > end Pack;
>
> > package Pack.Sub is
> >   procedure Proc2 (Var : in Typ);
> > end Pack.Sub;
>
> > with Pack.Sub;
> > procedure Proc is
> >   Obj : Pack.Typ;
> > begin
> >   Obj.Proc1;
> >   Obj.Proc2;
> > end Proc;
>
> > The second call does not compile (with gnat):
> > proc.adb:6:04: no selector "Proc2" for type "Typ" defined at pack.ads:
> > 3
>
> Yes, this is normal because Proc2 is not a primitive operation of Typ
> (or, in other words, it is not a method of Typ).  A primitive
> operation, by definition, is declared in the same package as its type.

"Primitive operation" is the wrong concept here.  First of all, the
subprogram only has to be declared in the same declarative region as
the type, or an ancestor of the type.  The "same declarative region"
can be in a procedure body or package body, which makes them not
primitive operations since primitive operations have to be in a
package specfication.  Also, the subprogram can be a subprogram whose
first parameter is Typ'Class; those are not primitive operations of
Typ, but they can be used in Object.Operation notation.  Finally, a
subprogram can be a primitive operation of Typ even if its second
parameter (or third, etc.) has the right type, or if it's a function
that returns Typ; but it can't be used in Object.Operation notation
unless the *first* parameter has the right type.

But in this case, yes, since Proc2 isn't declared in the same package
as Typ, it can't be used.

                                  -- Adam




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

* Re: Object.Operation and child package
  2008-06-25 19:03   ` Adam Beneschan
@ 2008-06-26  5:56     ` pascal.malaise
  0 siblings, 0 replies; 5+ messages in thread
From: pascal.malaise @ 2008-06-26  5:56 UTC (permalink / raw)


Thank you all for the explanation.
For the record, the most appropriate solution seems to be:

package Pack.Sub is
  type Sub_Typ is new Typ with private;
  procedure Proc2 (Var : in Sub_Typ);
private
  type Sub_Typ is new Typ with null record;
end Pack.Sub;

with Pack.Sub;
procedure Proc is
  O : Pack.Sub.Sub_Typ;
begin
  O.Proc1;
  O.Proc2;
end Proc;





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

end of thread, other threads:[~2008-06-26  5:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-06-25 17:41 Object.Operation and child package pascal.malaise
2008-06-25 18:40 ` Ludovic Brenta
2008-06-25 19:03   ` Adam Beneschan
2008-06-26  5:56     ` pascal.malaise
2008-06-25 18:51 ` Jeffrey R. Carter

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