comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Generic body
Date: Wed, 14 May 2008 15:03:31 -0700 (PDT)
Date: 2008-05-14T15:03:31-07:00	[thread overview]
Message-ID: <fb5fc137-6ac8-474d-ae0c-7184df7d7a6a@u36g2000prf.googlegroups.com> (raw)
In-Reply-To: g0fash$73v$1@registered.motzarella.org

On May 14, 11:26 am, Sébastien <seb.mor...@gmail.com> wrote:
> Hi,
>
> I'm trying to create a generic procedure with some instance in the same
> package.
>
> package MyPack is
>
>    generic
>      type Toto is limited private;
>    procedure MyProc;
>
>    procedure MyProcInt is new MyProc(Toto => Integer);
>    procedure MyProcStr is new MyProc(Toto => String);

This one can't be right in any case, because String is an
unconstrained type.  You can't declare a *variable*

    X : String;

so therefore you can't use String as the actual type for Toto, because
MyProc could declare a variable

    X : Toto;

somewhere in it.  But if you really need the ability to use "String"
here, do this in your generic:

    type Toto(<>) is limited private;

I realize this has nothing to do with your question.

>
> end MyPack;
>
> package body MyPack is
>
>    procedure MyProc is
>    begin
>      -- Some stuff
>    end MyProc;
>
> end MyPack;

Regarding your question: you really can't do this in Ada, because of
3.11(13-14): "For the instantiation of a generic unit that has a body,
a check is made that this body is already elaborated....The exception
Program_Error is raised if any of these checks fails."  The compiler
*should* accept your program with no errors, but it should fail at
runtime because the program will have to "elaborate" the generic
instantiation before it elaborates the generic body, and the program
will fail on Program_Error before it even gets started.  If your
compiler is rejecting your program, then either it is doing so because
it "knows" that the program can't possibly work when it runs, or it
has a bug.

Although there may be ways to suppress the elaboration check,
depending on your compiler, I wouldn't take that approach, because the
check is there for a reason.  If you suppress the check, there's a
possibility that your program may not work as expected, depending on
what the body of your generic looks like, especially if it refers to
any global variables in the body of MyPack.

I'd recommend putting the generic in its own library unit, or in a
different package:

   package MyProcPack is  -- I hate this name
     generic
       type Toto(<>) is limited private;
     procedure MyProc;
   end MyProcPack;

   package body MyProcPack is
     procedure MyProc is ... end MyProc;
   end MyProcPack;

   with MyProcPack;
   pragma Elaborate_All (MyProcPack);
   package MyPack is
     procedure MyProcInt is new MyProcPack.MyProc(Toto => Integer);
     procedure MyProcStr is new MyProcPack.MyProc(Toto => String);
   end MyPack;

The Elaborate_All pragma is necessary to ensure that the body of
MyProcPack (including the body of the generic MyProc) is elaborated
before MyProc is instantiated.

If you don't understand what elaboration is all about, it's too
difficult to explain here---sorry.  But just trust me that you'll need
to do something like what I'm recommending.


> I don't how to do this because:
> 1) The compiler refuse the instance of MyProc because it doesn't have
> the MyProc body

As I've mentioned, the compiler really shouldn't refuse this, because
it's a legal Ada program although it cannot work right.  Some
compilers have modes that cause them to reject programs that are
certain to raise predefined exceptions, even when the programs are
legal, and your compiler could be running in a mode like this.

> 2) If I put the body of MyProc in the package spec, the compiler refuses
> it because it's not the right place.

Right---you can't put any sort of body in a package spec.

                                    -- Adam





  parent reply	other threads:[~2008-05-14 22:03 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-14 18:26 Generic body Sébastien
2008-05-14 21:49 ` Randy Brukardt
2008-05-14 21:51 ` Samuel Tardieu
2008-05-14 22:03 ` Adam Beneschan [this message]
2008-05-15  9:25   ` Sébastien
2008-05-15 14:59     ` Adam Beneschan
2008-05-15 15:40       ` Sébastien
2008-05-15  8:43 ` gautier_niouzes
2008-05-15 13:11   ` Sébastien
replies disabled

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