comp.lang.ada
 help / color / mirror / Atom feed
* Ada 95 constructors on limited types
@ 2008-01-02 14:31 shaunpatterson
  2008-01-02 14:49 ` Dmitry A. Kazakov
                   ` (3 more replies)
  0 siblings, 4 replies; 11+ messages in thread
From: shaunpatterson @ 2008-01-02 14:31 UTC (permalink / raw)


I am trying to write a constructor that will return an access type to
a new object.

What I what is something like:

procedure Test is

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

begin
   null;
end Test;




Parent.ads:

package Parent is

   type Class is abstract tagged limited null record;
   type Class_Access is access all Class'Class;

end Parent;

package Parent.Child is

   type Class is new Parent.Class with private;

     function Create (Test_Value : Integer) return Class_Access;
--   function Create (Test_Value : Integer) return Class;


private
   type Class is new Parent.Class with
     record
        Value : Integer := 0;
     end record;

end Parent.Child;


package body Parent.Child is

    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;

--  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);


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

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

--
Shaun








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

* Re: Ada 95 constructors on limited types
  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
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 11+ messages in thread
From: Dmitry A. Kazakov @ 2008-01-02 14:49 UTC (permalink / raw)


On Wed, 2 Jan 2008 06:31:39 -0800 (PST), shaunpatterson@gmail.com wrote:

> I am trying to write a constructor that will return an access type to
> a new object.
> 
> What I what is something like:
> 
> procedure Test is
> 
>   Test_Pointer : Parent.Class_Access := new Parent.Child.Create (5);

Test_Pointer := Parent.Child.Create (5);

> begin
>    null;
> end Test;
> 
> Parent.ads:
> 
> package Parent is
> 
>    type Class is abstract tagged limited null record;
>    type Class_Access is access all Class'Class;

I would not use "access all" unless you need some instances of Class
allocated on the stack. But that's aside.

> end Parent;
> 
> package Parent.Child is
> 
>    type Class is new Parent.Class with private;
> 
>      function Create (Test_Value : Integer) return Class_Access;
> --   function Create (Test_Value : Integer) return Class;
> 
> private
>    type Class is new Parent.Class with
>      record
>         Value : Integer := 0;
>      end record;
> 
> end Parent.Child;
> 
> package body Parent.Child is
> 
>     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

   Result : Class_Access := new Class;  -- Allocation
   This : Class renames Class (Result.all); -- Downcasting
begin
   This.Value := Test_Value; -- Initialization
   return Result;

>     end Create;
> 
> end Parent.Child;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: Ada 95 constructors on limited types
  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
  2008-01-02 16:20 ` Jeffrey R. Carter
  2008-01-02 18:36 ` Martin Krischik
  3 siblings, 0 replies; 11+ messages in thread
From: Robert A Duff @ 2008-01-02 16:02 UTC (permalink / raw)


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



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

* Re: Ada 95 constructors on limited types
  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
@ 2008-01-02 16:20 ` Jeffrey R. Carter
  2008-01-02 22:57   ` Brian May
  2008-01-02 18:36 ` Martin Krischik
  3 siblings, 1 reply; 11+ messages in thread
From: Jeffrey R. Carter @ 2008-01-02 16:20 UTC (permalink / raw)


shaunpatterson@gmail.com wrote:
> 
> package Parent is
> 
>    type Class is abstract tagged limited null record;
>    type Class_Access is access all Class'Class;
> 
> end Parent;

There's your problem. Don't use public access types.

-- 
Jeff Carter
"Run away! Run away!"
Monty Python and the Holy Grail
58



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

* Re: Ada 95 constructors on limited types
  2008-01-02 14:31 Ada 95 constructors on limited types shaunpatterson
                   ` (2 preceding siblings ...)
  2008-01-02 16:20 ` Jeffrey R. Carter
@ 2008-01-02 18:36 ` Martin Krischik
  2008-01-04  1:38   ` Randy Brukardt
  3 siblings, 1 reply; 11+ messages in thread
From: Martin Krischik @ 2008-01-02 18:36 UTC (permalink / raw)


shaunpatterson@gmail.com wrote:

> I am trying to write a constructor that will return an access type to
> a new object.

Three month into learning Ada I removed 90% of all access types from my code
because I discovered that access types are seldom needed in Ada.

Read;

http://en.wikibooks.org/wiki/Ada_Programming/Object_Orientation#The_class-wide_type

> type Class_Access is access all Class'Class;

Do you need that "all"? Because because the "all" does not come for free -
there are some penalties.

Read:

http://en.wikibooks.org/wiki/Ada_Programming/Types/access#Access_vs._access_all

Apart from that Dimity answered your question.

Regards

Marin
-- 
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com



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

* Re: Ada 95 constructors on limited types
  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
  0 siblings, 2 replies; 11+ messages in thread
From: Brian May @ 2008-01-02 22:57 UTC (permalink / raw)


>>>>> "Jeffrey" == Jeffrey R Carter <spam.jrcarter.not@acm.nospam.org> writes:

    Jeffrey> shaunpatterson@gmail.com wrote:
    >> 
    >> package Parent is
    >> 
    >> type Class is abstract tagged limited null record;
    >> type Class_Access is access all Class'Class;
    >> 
    >> end Parent;

    Jeffrey> There's your problem. Don't use public access types.

Why not?
-- 
Brian May <bam@snoopy.apana.org.au>



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

* Re: Ada 95 constructors on limited types
  2008-01-02 22:57   ` Brian May
@ 2008-01-02 23:53     ` Jeffrey R. Carter
  2008-01-05 10:32     ` Ludovic Brenta
  1 sibling, 0 replies; 11+ messages in thread
From: Jeffrey R. Carter @ 2008-01-02 23:53 UTC (permalink / raw)


Brian May wrote:
>>>>>> "Jeffrey" == Jeffrey R Carter <spam.jrcarter.not@acm.nospam.org> writes:
> 
>     Jeffrey> There's your problem. Don't use public access types.
> 
> Why not?

For the same kind of reasons that you don't use goto or C.

-- 
Jeff Carter
"Run away! Run away!"
Monty Python and the Holy Grail
58



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

* Re: Ada 95 constructors on limited types
  2008-01-02 18:36 ` Martin Krischik
@ 2008-01-04  1:38   ` Randy Brukardt
  0 siblings, 0 replies; 11+ messages in thread
From: Randy Brukardt @ 2008-01-04  1:38 UTC (permalink / raw)


"Martin Krischik" <krischik@users.sourceforge.net> wrote in message
news:1379898.fuxZSb5qdF@linux1.krischik.com...
...
> > type Class_Access is access all Class'Class;
>
> Do you need that "all"? Because because the "all" does not come for free -
> there are some penalties.

Say what? The only penalty in using "all" is that a few very dubious
constructs are illegal (they're legal for pool-specific types in order to
maintain Ada 83 compatibility. Indeed, for Janus/Ada, it actually makes the
code better (because we generate code that checks that pool-specific access
values belong to their pool when they are dereferenced; for obvious reasons
we can't do that for "all" or for user-defined pools).

It also makes your code better as it allows you to avoid using allocators at
all.

Of course the very best solution is to avoid the access type altogether,
which you may be able to do by using one of the containers libraries.

> Read:
>
>
http://en.wikibooks.org/wiki/Ada_Programming/Types/access#Access_vs._access_all

The claim that additional checks may be needed on the deference of an
"access all" is completely bogus; Janus/Ada always does *less* run-time
checking for "access all" than "access". While it's probably true that for
most compilers that it is the same, I can't think of any reason that it
would be more -- at least not on typical machines. (There might be
additional checks required because of the designated type, but those would
be the same for access and access all.) One could imagine a super-safe Ada
system that did extra checks to prevent problems with
Unchecked_Deallocation, but that would not have any cost for dereferencing.

The argument about safety is certainly true, but it may be misleading:
Unchecked_Deallocation is always dangerous if misused. It is just as easy to
deallocate a pool-specific object twice, and just as dangerous as
deallocating a stack object. The advantage of "access all" is that you may
not need to use Unchecked_Deallocation at all.

Moral: if you have (or may have) a valid reason to store an 'Access or
'Unchecked_Access into an access type, then use "access all" and don't worry
about it. If not, the mantra of "least privilege" suggests that the "all"
should be left out (don't enable capabilities that you are not going to
use), but it is hardly the level of concern that it should cause someone to
complain about it's use in an example program.

OTOH, the advice about avoiding access altogether for parameters, and
especially anonymous access, is spot-on.

                                                Randy.





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

* Re: Ada 95 constructors on limited types
  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
  1 sibling, 1 reply; 11+ messages in thread
From: Ludovic Brenta @ 2008-01-05 10:32 UTC (permalink / raw)


Brian May writes:
>>>>>> "Jeffrey" == Jeffrey R Carter <spam.jrcarter.not@acm.nospam.org> writes:
>
>     Jeffrey> shaunpatterson@gmail.com wrote:
>     >> 
>     >> package Parent is
>     >> 
>     >> type Class is abstract tagged limited null record;
>     >> type Class_Access is access all Class'Class;
>     >> 
>     >> end Parent;
>
>     Jeffrey> There's your problem. Don't use public access types.
>
> Why not?

Because they automatically make the package impure for no good reason,
thereby preventing use of the package in pure or preelabotated
packages.  Moral: Declare your access types only if and where you need
them.

-- 
Ludovic Brenta.



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

* Re: Ada 95 constructors on limited types
  2008-01-05 10:32     ` Ludovic Brenta
@ 2008-01-05 14:59       ` Robert A Duff
  2008-01-08  1:58         ` Randy Brukardt
  0 siblings, 1 reply; 11+ messages in thread
From: Robert A Duff @ 2008-01-05 14:59 UTC (permalink / raw)


Ludovic Brenta <ludovic@ludovic-brenta.org> writes:

> Brian May writes:
>>>>>>> "Jeffrey" == Jeffrey R Carter <spam.jrcarter.not@acm.nospam.org> writes:
>>
>>     Jeffrey> shaunpatterson@gmail.com wrote:
>>     >> 
>>     >> package Parent is
>>     >> 
>>     >> type Class is abstract tagged limited null record;
>>     >> type Class_Access is access all Class'Class;
>>     >> 
>>     >> end Parent;
>>
>>     Jeffrey> There's your problem. Don't use public access types.
>>
>> Why not?
>
> Because they automatically make the package impure for no good reason,
> thereby preventing use of the package in pure or preelabotated
> packages.  Moral: Declare your access types only if and where you need
> them.

Jeff was complaining only about PUBLIC access types.
I don't entirely agree with him -- at least, I'd put
it less strongly.

But anyway, the purity issue applies to access types
whether they are public or not.  I agree that it's
good to make things Pure when possible.

Note that the rule was relaxed in Ada 2005 -- you can say:

    type T is access Blah;
    for T'Storage_Size use 0;

and still have pragma Pure.  You can also have access-to-constant
and access-to-subprogram types.

- Bob



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

* Re: Ada 95 constructors on limited types
  2008-01-05 14:59       ` Robert A Duff
@ 2008-01-08  1:58         ` Randy Brukardt
  0 siblings, 0 replies; 11+ messages in thread
From: Randy Brukardt @ 2008-01-08  1:58 UTC (permalink / raw)


"Robert A Duff" <bobduff@shell01.TheWorld.com> wrote in message
news:wccy7b4xqro.fsf@shell01.TheWorld.com...
> But anyway, the purity issue applies to access types
> whether they are public or not.  I agree that it's
> good to make things Pure when possible.
>
> Note that the rule was relaxed in Ada 2005 -- you can say:
>
>     type T is access Blah;
>     for T'Storage_Size use 0;
>
> and still have pragma Pure.  You can also have access-to-constant
> and access-to-subprogram types.

Ummm, that isn't very useful (that's a type for which there never can be an
object!). I think you meant:

     type T is access all Blah;
     for T'Storage_Size use 0;

...which can be useful. But note that that the point of setting the storage
size to 0 is to make allocators illegal - which greatly restricts what you
can do. Better to avoid the access type altogether.

                                      Randy.






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

end of thread, other threads:[~2008-01-08  1:58 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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