comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Programming by interface in Ada
Date: Wed, 13 Aug 2008 08:31:54 -0700 (PDT)
Date: 2008-08-13T08:31:54-07:00	[thread overview]
Message-ID: <7e79c4ad-d196-4372-8974-6de9b9baaea1@v39g2000pro.googlegroups.com> (raw)
In-Reply-To: g7ufm7$p77$1@registered.motzarella.org

On Aug 13, 4:14 am, Sébastien Morand <seb.mor...@gmail.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi,
>
> I'm trying to do the following thing:
>
> - --- pkginterface.ads -------------------------------
> with ObjInterface; use ObjInterface;
>
> package PkgInterface is
>
>    --  Mon interface class
>    type MonInterface is interface;
>    type MonInterfacePtr is access all MonInterface;
>
>    procedure SetObjet(r: in out MonInterface; c: in MonCount; o: in
> MonObjetPtr) is abstract;
>
> end PkgInterface;
>
> - --- objinterface.ads -------------------------------
> package ObjInterface is
>
>    --  Mon interface class
>    type MonObjet is interface;
>    type MonObjetPtr is access all MonObjet;
>
> end ObjInterface;
>
> - --- pkgimpl.ads -------------------------------
> with PkgInterface; use PkgInterface;
> with ObjInterface; use ObjInterface;
>
> package PkgImpl is
>
>    type MonImpl is new MonInterface with private;
>    type MonImplPtr is access all MonImpl;
>
>    overriding
>    procedure SetObjet(r: in out MonImpl; c: in MonCount; o: in MonObjetPtr);
>
> private
>
>    type MonImpl is new MonInterface with record
>       a: MonObjetPtr;
>    end record;
>
> end PkgImpl;
>
> - --- pkgimpl.adb -------------------------------
> package body PkgImpl is
>
>    procedure SetObjet(r: in out MonImpl; c: in MonCount; o: in
> MonObjetPtr) is
>    begin
>       r.a := o;
>    end SetObjet;
>
> end PkgImpl;
> - ----------------------------------
>
> When I'm trying to compile pkgimpl.adb I get the following error:
> pkgimpl:5:04: (Ada 2005) : abstract interface primitives must be defined
> in package specs.
>
> The interface primitive it's yelling about should be SetObjet which is
> correctly defined in the spec. So waht's the matter? It looks like the
> MonObjetPtr (access all ObjInterface) is the real problem.
>
> How can I achieve this?
>
> Thanks by advance,

Well, I've found that if you change

  type MonObjetPtr is access all MonObjet;

to

  type MonObjetPtr is access all MonObjet'Class;

the error goes away.  There is no Ada reason why this should be so.  I
think your program is legal and GNAT has a bug.  Maybe it's
incorrectly applying a rule for anonymous-access parameters to your
MonObjetPtr parameter.

But I also see no reason to use "access all MonObjet;" anyway, without
the 'Class.  Since you can't have objects of type MonObjet, declaring
an access to this object (rather than to MonObjet'Class) doesn't seem
worthwhile.  I think you can make it work by type conversions---you
can convert an "access all MonObjet'Class" to an "access all MonObjet"
and vice versa, but you can't use an "access all MonObjet" without all
those type conversions, so there doesn't seem to be much point in
declaring it that way.

So that's what I'd do.  And the same thing with MonInterfacePtr.

By the way, may I make a request of anyone who posts this sort of
question?  If it involves a GNAT error, please do something extra to
indicate what line the error is occurring on.  The error message in
the post indicated that the error was on line 5, but that's not
helpful at all---I can't tell from the post which line is line 5.
(For one thing, one of the Ada lines got line-wrapped by my
newsreader.  For another thing, I can't tell which of the comment
lines are actually part of the source file.)  Of course, I can figure
out what's going on by cutting-and-pasting the source and trying it
myself, but that's extra work, plus it's not always feasible if there
are missing declarations (e.g. you didn't include the declaration of
MonCount in the above source, although that was easy to fix).  Maybe
this is too much to ask, but I do ask that posters be aware that our
newsreaders will not always display the source exactly as your
compiler sees it.  Thanks.

                                -- Adam





  reply	other threads:[~2008-08-13 15:31 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-13 11:14 Programming by interface in Ada Sébastien Morand
2008-08-13 15:31 ` Adam Beneschan [this message]
2008-08-13 16:08   ` Marcus Lauster
2008-08-13 17:00   ` Robert A Duff
2008-08-13 17:42   ` Ludovic Brenta
replies disabled

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