comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Ada 95 constructors on limited types
Date: Wed, 02 Jan 2008 11:02:03 -0500
Date: 2008-01-02T11:02:03-05:00	[thread overview]
Message-ID: <wcc7iistdvo.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: 08dc2b30-6c8c-4cff-9f2e-c0d4c377972d@i3g2000hsf.googlegroups.com

shaunpatterson@gmail.com writes:

>     function Create (Test_Value : Integer) return Class_Access is
>     begin
>          return new Class' (Value => Test_Value);
>          -- Works only with Ada 2005... I am stuck using Ada 95
>     end Create;

Right.  Dmitry showed how to write it.

> --  function Create (Test_Value : Integer) return Class is
> --    begin
> --       return Class' (Value => Test_Value);
> --    end Create;
>
> end Parent.Child;
>
>
> So, the first create works in Ada2005 with:
>
>   Test_Pointer : Parent.Class_Access := Parent.Child.Create (5);
>
> I have also tried the second Create unsuccessfully with
>
>   Test_Pointer : Parent.Class_Access := new Parent.Child.Create (5);

That's not the right syntax for an allocator.  See RM-4.8(2),
or any Ada textbook.  (The allocator in the first Create
above uses the right syntax for an initialized allocator;
Dmitry showed an uninitialized allocator.)

In Ada 2005, you can say:

Test_Pointer : Parent.Class_Access
   := new Parent.Child.Class'(Parent.Child.Create (5));

Or if you say "use Parent, Parent.Child;":

Test_Pointer : Parent.Class_Access
   := new Child.Class'(Create (5));

This is illegal in Ada 95.  In fact, the second Create
function won't work in Ada 95 even without the allocator.
You can make it legal by making the type nonlimited.

Limited types are rather limited (so to speak) in Ada 95, because you're
not allowed to explicitly initialize objects (as the above allocator
does).  This annoying restriction is removed in Ada 2005.

> I don't understand how to initialize limited types and their pointers.

You can use default initialization.  You can use a discriminant
to pass information to the default.  You can initialize nonlimited
components of a limited object, as Dmitry showed.  You can switch
to nonlimited types.  Or you can switch to Ada 2005.

Here's an example with a discriminant:

   type Class (Initial_Value : Integer) is new Parent.Class with private;
   ...
private
   type Class  (Initial_Value : Integer) is new Parent.Class with
     record
        Value : Integer := Initial_Value; -- Or some expression
                                          -- like Func(Initial_Value)*2
                                          -- would be legal, too.
     end record;

Then you can say:

    ... := new Parent.Child.Class(Initial_Value => 5);

and the heap object's Value component will be default-initialized
to 5.

> Any help?  Again, my project currently is tied down to Ada95 (gnat
> 3.16p)

Sorry to hear that.  3.16p is quite old!

- Bob



  parent reply	other threads:[~2008-01-02 16:02 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-01-02 14:31 Ada 95 constructors on limited types shaunpatterson
2008-01-02 14:49 ` Dmitry A. Kazakov
2008-01-02 16:02 ` Robert A Duff [this message]
2008-01-02 16:20 ` Jeffrey R. Carter
2008-01-02 22:57   ` Brian May
2008-01-02 23:53     ` Jeffrey R. Carter
2008-01-05 10:32     ` Ludovic Brenta
2008-01-05 14:59       ` Robert A Duff
2008-01-08  1:58         ` Randy Brukardt
2008-01-02 18:36 ` Martin Krischik
2008-01-04  1:38   ` Randy Brukardt
replies disabled

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