From: Adam Beneschan <adam@irvine.com>
Subject: Re: Renaming of procedures in a generic instantiation
Date: Mon, 27 Sep 2010 12:23:00 -0700 (PDT)
Date: 2010-09-27T12:23:00-07:00 [thread overview]
Message-ID: <30fadcf5-9a20-46b9-b5ef-8a657fb98256@m35g2000prn.googlegroups.com> (raw)
In-Reply-To: f0affc67-0080-410f-9595-b91f893ec6f5@a19g2000yql.googlegroups.com
On Sep 25, 5:43 pm, Gene <gene.ress...@gmail.com> wrote:
> I'm confused about an aspect of renaming. It boils out to this little
> example:
>
> with Ada.Containers.Ordered_Sets;
>
> package Foo is
>
> type Queue is private;
>
> procedure Add(Q : in out Queue; Item : in Integer);
>
> function Is_Empty(Q : Queue) return Boolean;
>
> private
>
> package Queues is
> new Ada.Containers.Ordered_Sets(Integer, "<", "=");
> type Queue is new Queues.Set with null record;
>
> end Foo;
>
> package body Foo is
>
> procedure Add(Q : in out Queue; Item : in Integer)
> renames Insert;
>
> function Is_Empty(Q : Queue) return Boolean
> renames Queues.Is_Empty;
>
> end Foo;
>
> The renaming in Add "finds" the correct procedure Insert, but the
> renaming of Is_Empty fails:
>
> gnatmake foo.adb
> gcc -c foo.adb
> foo.adb:6:04: no visible subprogram matches the specification for
> "Is_Empty"
> foo.adb:6:13: expected private type "Ada.Containers.Ordered_Sets.Set"
> from insta
> nce at foo.ads:14
> foo.adb:6:13: found type "Queue" defined at foo.ads:16
> gnatmake: "foo.adb" compilation error
>
> I guess I can see this error because a parameter type conversion is
> implied in the renaming. But then why does the renaming of Insert
> work correctly?
Because you didn't refer to the "Insert" defined in Queues, the way
you did with Is_Empty. If you had declared this, it would be illegal
in the same way:
procedure Add(Q : in out Queue; Item : in Integer)
renames Queues.Insert;
Obviously, you can't solve the problem for Is_Empty by removing the
"Queues." prefix:
function Is_Empty(Q : in out Queue) return Boolean
renames Is_Empty;
I found that this compiles, but it's obnoxious:
with Ada.Containers.Ordered_Sets;
package Foo is
type Queue is private;
procedure Add(Q : in out Queue; Item : in Integer);
function Is_Empty(Q : Queue) return Boolean;
private
package Queues is
new Ada.Containers.Ordered_Sets(Integer, "<", "=");
package Dummy_Package is
type Dummy_Queue is new Queues.Set with null record;
function Rename_Empty(Q : Dummy_Queue) return Boolean
renames Is_Empty;
end Dummy_Package;
type Queue is new Dummy_Package.Dummy_Queue with null record;
end Foo;
package body Foo is
procedure Add(Q : in out Queue; Item : in Integer)
renames Insert;
function Is_Empty(Q : Queue) return Boolean
renames Rename_Empty;
end Foo;
Just writing Is_Empty to call Queues.Is_Empty, as Niklas suggested,
seems better than this. I'd also add "pragma Inline(Is_Empty)" to the
private part of Foo to possibly prevent extraneous call code from
being generated.
I had thought that someone (possibly me) once proposed adding the
ability to declare that a subprogram renames the hidden subprogram
that it overrides, e.g.
function Is_Empty(Q : Queue) return Boolean renames <>;
but I can't find anything like that in my mail records. Randy did
once mention using something like Is_Empty'Parent as a stand-in for
the call to the parent routine; he wasn't thinking of a renaming
context, but it might work here:
function Is_Empty(Q : Queue) return Boolean renames
Is_Empty'Parent;
In any event, however, it seems to be a minor enough problem with a
simple enough workaround that it's not worthwhile to change the
language.
-- Adam
prev parent reply other threads:[~2010-09-27 19:23 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-09-26 0:43 Renaming of procedures in a generic instantiation Gene
2010-09-26 6:54 ` Niklas Holsti
2010-09-26 7:40 ` Jeffrey Carter
2010-09-26 8:41 ` Niklas Holsti
2010-09-26 17:07 ` Jeffrey Carter
2010-09-26 14:52 ` Gene
2010-09-26 15:04 ` Dmitry A. Kazakov
2010-09-26 8:45 ` Stephen Leake
2010-09-26 9:11 ` Niklas Holsti
2010-09-27 1:18 ` Gene
2010-09-28 11:36 ` Stephen Leake
2010-09-29 1:25 ` Gene
2010-09-27 19:23 ` Adam Beneschan [this message]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox