comp.lang.ada
 help / color / mirror / Atom feed
* Declaring private packages to instantiate generics?
@ 2012-05-16 14:21 Markus Schöpflin
  2012-05-16 14:36 ` Adam Beneschan
  0 siblings, 1 reply; 7+ messages in thread
From: Markus Schöpflin @ 2012-05-16 14:21 UTC (permalink / raw)


I just found myself wanting to write the following code, which Gnat doesn't like:

---%<---
package FOO is
   procedure P;
end FOO;

package body FOO is
   package BAR is new SOME_GENERIC_PACKAGE;
   procedure P is new BAR.P;
end FOO;
--->%---

Can I somehow provide the body of P without writing a procedure which just 
forwards to an instantiation of BAR.P, IOW without having to write:

---%<---
package body FOO is
   package BAR is new SOME_GENERIC_PACKAGE;
   procedure BAR_P is new BAR.P;
   procedure P is begin BAR_P; end;
end FOO;
--->%---

Regards,
Markus



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

* Re: Declaring private packages to instantiate generics?
  2012-05-16 14:21 Declaring private packages to instantiate generics? Markus Schöpflin
@ 2012-05-16 14:36 ` Adam Beneschan
  2012-05-16 14:45   ` Markus Schöpflin
  2012-05-17  3:37   ` Adam Beneschan
  0 siblings, 2 replies; 7+ messages in thread
From: Adam Beneschan @ 2012-05-16 14:36 UTC (permalink / raw)


On Wednesday, May 16, 2012 7:21:24 AM UTC-7, Markus Schöpflin wrote:
> I just found myself wanting to write the following code, which Gnat doesn't like:
> 
> ---%<---
> package FOO is
>    procedure P;
> end FOO;
> 
> package body FOO is
>    package BAR is new SOME_GENERIC_PACKAGE;
>    procedure P is new BAR.P;
> end FOO;
> --->%---
> 
> Can I somehow provide the body of P without writing a procedure which just 
> forwards to an instantiation of BAR.P, IOW without having to write:
> 
> ---%<---
> package body FOO is
>    package BAR is new SOME_GENERIC_PACKAGE;
>    procedure BAR_P is new BAR.P;
>    procedure P is begin BAR_P; end;
> end FOO;
> --->%---
> 
> Regards,
> Markus

You can change the last to

   procedure P renames BAR_P;

But that's the best you can do.  If you have an incomplete declaration (i.e. the declaration of P in the *spec* of FOO), you can't complete it with a generic instantiation---that's the rule.  (I think he reason is that a generic instantiation is equivalent to a declaration of the instance followed by the body of the instance, and doing that here would mean you're declaring P twice, which isn't allowed.  It can be argued that the language rules should be relaxed in this case, since it's tripped up more than one person, and offhand I don't see any complications that would arise if it were allowed.)

                          -- Adam



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

* Re: Declaring private packages to instantiate generics?
  2012-05-16 14:36 ` Adam Beneschan
@ 2012-05-16 14:45   ` Markus Schöpflin
  2012-05-17  1:24     ` Shark8
  2012-05-17  3:37   ` Adam Beneschan
  1 sibling, 1 reply; 7+ messages in thread
From: Markus Schöpflin @ 2012-05-16 14:45 UTC (permalink / raw)


Am 16.05.2012 16:36, schrieb Adam Beneschan:

[...]

>> ---%<---
>> package body FOO is
>>     package BAR is new SOME_GENERIC_PACKAGE;
>>     procedure BAR_P is new BAR.P;
>>     procedure P is begin BAR_P; end;
>> end FOO;
>> --->%---
>>
>> Regards,
>> Markus
>
> You can change the last to
>
>     procedure P renames BAR_P;

Thanks for your answer. Not as nice as I had hoped, but basically what I was 
looking for.

Markus



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

* Re: Declaring private packages to instantiate generics?
  2012-05-16 14:45   ` Markus Schöpflin
@ 2012-05-17  1:24     ` Shark8
  0 siblings, 0 replies; 7+ messages in thread
From: Shark8 @ 2012-05-17  1:24 UTC (permalink / raw)


On Wednesday, May 16, 2012 9:45:20 AM UTC-5, Markus Schöpflin wrote:
> Am 16.05.2012 16:36, schrieb Adam Beneschan:
> 
> [...]
> 
> >> ---%<---
> >> package body FOO is
> >>     package BAR is new SOME_GENERIC_PACKAGE;
> >>     procedure BAR_P is new BAR.P;
> >>     procedure P is begin BAR_P; end;
> >> end FOO;
> >> --->%---
> >>
> >> Regards,
> >> Markus
> >
> > You can change the last to
> >
> >     procedure P renames BAR_P;
> 
> Thanks for your answer. Not as nice as I had hoped, but basically what I was 
> looking for.
> 
> Markus

Why is using RENAMES not nice?
It seems that the only difference between using RENAMES and what you showed (procedure P is new BAR.P;) is the syntax.



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

* Re: Declaring private packages to instantiate generics?
  2012-05-16 14:36 ` Adam Beneschan
  2012-05-16 14:45   ` Markus Schöpflin
@ 2012-05-17  3:37   ` Adam Beneschan
  2012-05-21  8:06     ` Markus Schöpflin
  1 sibling, 1 reply; 7+ messages in thread
From: Adam Beneschan @ 2012-05-17  3:37 UTC (permalink / raw)


On Wednesday, May 16, 2012 7:36:27 AM UTC-7, Adam Beneschan wrote:

> > package FOO is
> >    procedure P;
> > end FOO;
> > 
> > package body FOO is
> >    package BAR is new SOME_GENERIC_PACKAGE;
> >    procedure P is new BAR.P;
> > end FOO;
> > --->%---
> > 
> > Can I somehow provide the body of P without writing a procedure which just 
> > forwards to an instantiation of BAR.P, IOW without having to write:
> > 
> > ---%<---
> > package body FOO is
> >    package BAR is new SOME_GENERIC_PACKAGE;
> >    procedure BAR_P is new BAR.P;
> >    procedure P is begin BAR_P; end;
> > end FOO;
> > --->%---
> > 
> > Regards,
> > Markus
> 
> You can change the last to
> 
>    procedure P renames BAR_P;
> 
> But that's the best you can do.  If you have an incomplete declaration (i.e. the declaration of P in the *spec* of FOO), you can't complete it with a generic instantiation---that's the rule.  (I think he reason is that a generic instantiation is equivalent to a declaration of the instance followed by the body of the instance, and doing that here would mean you're declaring P twice, which isn't allowed.  It can be argued that the language rules should be relaxed in this case, since it's tripped up more than one person, and offhand I don't see any complications that would arise if it were allowed.)

I just got reminded by Tucker Taft that there's a problem if P has parameters.  The parameters aren't listed in a generic_instantiation:

  package FOO is
     procedure P (Param : in Integer; Param2 : out Integer);
  end FOO;
 
  package body FOO is
     package BAR is new SOME_GENERIC_PACKAGE;
     procedure P is new BAR.P;
  end FOO;

This makes it less clear to the eye that the second P completes the first one.  (This would especially be a problem if there is more than one P with different parameter profiles in the specification.)  This makes it clearer:

  package FOO is
     procedure P (Param : in Integer; Param2 : out Integer);
  end FOO;
 
  package body FOO is
     package BAR is new SOME_GENERIC_PACKAGE;
     procedure New_P is new BAR.P;
     procedure P (Param : in Integer; Param2 : out Integer)
        renames New_P;
  end FOO;

So there's a good reason why the rules are the way they are.

                      -- Adam



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

* Re: Declaring private packages to instantiate generics?
  2012-05-17  3:37   ` Adam Beneschan
@ 2012-05-21  8:06     ` Markus Schöpflin
  2012-05-21 15:51       ` Simon Wright
  0 siblings, 1 reply; 7+ messages in thread
From: Markus Schöpflin @ 2012-05-21  8:06 UTC (permalink / raw)


Am 17.05.2012 05:37, schrieb Adam Beneschan:
> On Wednesday, May 16, 2012 7:36:27 AM UTC-7, Adam Beneschan wrote:

[...]

> I just got reminded by Tucker Taft that there's a problem if P has
> parameters.  The parameters aren't listed in a generic_instantiation:
>
> package FOO is procedure P (Param : in Integer; Param2 : out Integer); end
> FOO;
>
> package body FOO is package BAR is new SOME_GENERIC_PACKAGE; procedure P is
> new BAR.P; end FOO;
>
> This makes it less clear to the eye that the second P completes the first
> one.  (This would especially be a problem if there is more than one P with
> different parameter profiles in the specification.)  This makes it
> clearer:
>
> package FOO is procedure P (Param : in Integer; Param2 : out Integer); end
> FOO;
>
> package body FOO is package BAR is new SOME_GENERIC_PACKAGE; procedure
> New_P is new BAR.P; procedure P (Param : in Integer; Param2 : out Integer)
> renames New_P; end FOO;
>
> So there's a good reason why the rules are the way they are.

You're right, thanks for the follow-up. And it's even the case in my case, I
have a whole bunch of Ps which are implemented as instantiations of the same
procedure.

Markus

PS: Sorry for rewrapping your part of the mail but my NNTP server started to 
complain about lines longer than 79 characters when posting.



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

* Re: Declaring private packages to instantiate generics?
  2012-05-21  8:06     ` Markus Schöpflin
@ 2012-05-21 15:51       ` Simon Wright
  0 siblings, 0 replies; 7+ messages in thread
From: Simon Wright @ 2012-05-21 15:51 UTC (permalink / raw)


Markus Schöpflin <no.spam@spam.spam> writes:

> PS: Sorry for rewrapping your part of the mail but my NNTP server
> started to complain about lines longer than 79 characters when
> posting.

I've done this for a long time as a matter of course. It's particularly
necessary now that Google Groups has stopped doing any line-wrap at all!
(and very easy using Emacs gnus - M-q).

I didn't think it was rude?



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

end of thread, other threads:[~2012-05-21 15:51 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-05-16 14:21 Declaring private packages to instantiate generics? Markus Schöpflin
2012-05-16 14:36 ` Adam Beneschan
2012-05-16 14:45   ` Markus Schöpflin
2012-05-17  1:24     ` Shark8
2012-05-17  3:37   ` Adam Beneschan
2012-05-21  8:06     ` Markus Schöpflin
2012-05-21 15:51       ` Simon Wright

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