comp.lang.ada
 help / color / mirror / Atom feed
* controlled types and point of instanciation
@ 2018-03-16 14:39 Mehdi Saada
  2018-03-16 16:31 ` J-P. Rosen
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Mehdi Saada @ 2018-03-16 14:39 UTC (permalink / raw)


I make the humble request of an exegesis of this text: (from ada 95 the craft of oriented programming)
"Since Ada.Finalization.Limited_Controlled and Ada.Finalization.Controlled are both library-level types, all controlled types must be derived by instantiation at library level. Since generic packages are treated as being declared at the point of instantiation, this means that JE.Lists can only be instantiated at library level, usually within the specification of another library package. This means that we can no longer use JE.Lists to build opaque types as [bellow]. Instantiating JE.Lists inside a package body is no longer permissible."

--> it refers to that:
package JE.Stacks is
-- anything
private
   type Stack_Item;
   type Stack_Type is access Stack_Item;
end JE.Stacks;

with JE.Lists;
package body JE.Stacks is
   package Lists is new JE.Lists (Item_Type);
   type Stack_Item is
   record
      L : Lists.List_Type;
   end record;
... end JE.STACKS;

So that I can grasp the rule better, what are the anomalous cases this rule wants to avoid ?

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

* Re: controlled types and point of instanciation
  2018-03-16 14:39 controlled types and point of instanciation Mehdi Saada
@ 2018-03-16 16:31 ` J-P. Rosen
  2018-03-16 17:14   ` Mehdi Saada
  2018-03-16 18:06 ` Mehdi Saada
  2018-03-17  0:54 ` Mehdi Saada
  2 siblings, 1 reply; 6+ messages in thread
From: J-P. Rosen @ 2018-03-16 16:31 UTC (permalink / raw)


Le 16/03/2018 à 15:39, Mehdi Saada a écrit :
> I make the humble request of an exegesis of this text: (from ada 95 the craft of oriented programming)
> "Since Ada.Finalization.Limited_Controlled and Ada.Finalization.Controlled are both library-level types, all controlled types must be derived by instantiation at library level. Since generic packages are treated as being declared at the point of instantiation, this means that JE.Lists can only be instantiated at library level, usually within the specification of another library package. This means that we can no longer use JE.Lists to build opaque types as [bellow]. Instantiating JE.Lists inside a package body is no longer permissible."

This was a rule of Ada95 intended to prevent accessing non existing
objects, but very annoying in practice, precisely because it forbad
instantiating the containers anywhere else than at library level.

Since Ada 2005, it has been replaced by a run-time check, so you don't
need to worry.

-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr

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

* Re: controlled types and point of instanciation
  2018-03-16 16:31 ` J-P. Rosen
@ 2018-03-16 17:14   ` Mehdi Saada
  0 siblings, 0 replies; 6+ messages in thread
From: Mehdi Saada @ 2018-03-16 17:14 UTC (permalink / raw)


Wow ! Fine, thanks. I would have missed that use of generics a lot.


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

* Re: controlled types and point of instanciation
  2018-03-16 14:39 controlled types and point of instanciation Mehdi Saada
  2018-03-16 16:31 ` J-P. Rosen
@ 2018-03-16 18:06 ` Mehdi Saada
  2018-03-16 21:44   ` Jeffrey R. Carter
  2018-03-17  0:54 ` Mehdi Saada
  2 siblings, 1 reply; 6+ messages in thread
From: Mehdi Saada @ 2018-03-16 18:06 UTC (permalink / raw)


By the way, I may ask something else related to pointers and this book. Does the preliminary test of this function seems ok ?

function Pointer (Value : Access_Type) return Pointer_Type is
    Object : Pointer_Type;
begin
    if Object.Pointer /= null then
       Delete_Item (Object.Pointer.Value);
    else
       Object.Pointer := new Reference_Counted_Object;
    end if;
    Object.Pointer.all := (Value => Value, Count => 1);
    return Object;
end Pointer;

That Pointer_type has no default values whatsoever, so it doesn't seems necessary to me.
its structure is
type Pointer_Type is new Ada.Finalization.Controlled with
record
     Pointer : Reference_Counted_Pointer;
end record;
and Pointer is a banal access type.
type Reference_Counted_Pointer is access Reference_Counted_Object;
type Reference_Counted_Object is
record
   Value : Access_Type; -- > from generic parameter, points at a non limited unconstrained type.
        type Access_Type is access Item_Type;
   Count : Natural;
end record;

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

* Re: controlled types and point of instanciation
  2018-03-16 18:06 ` Mehdi Saada
@ 2018-03-16 21:44   ` Jeffrey R. Carter
  0 siblings, 0 replies; 6+ messages in thread
From: Jeffrey R. Carter @ 2018-03-16 21:44 UTC (permalink / raw)


On 03/16/2018 07:06 PM, Mehdi Saada wrote:
> 
> function Pointer (Value : Access_Type) return Pointer_Type is
>      Object : Pointer_Type;
> begin
>      if Object.Pointer /= null then
>         Delete_Item (Object.Pointer.Value);
>      else
>         Object.Pointer := new Reference_Counted_Object;
>      end if;
>      Object.Pointer.all := (Value => Value, Count => 1);
>      return Object;
> end Pointer;
> 
> type Pointer_Type is new Ada.Finalization.Controlled with
> record
>       Pointer : Reference_Counted_Pointer;
> end record;
> and Pointer is a banal access type.
> type Reference_Counted_Pointer is access Reference_Counted_Object;

Oject.Pointer will always be null, so this seems unnecessarily complex. I'd 
probably just write

return new Reference_Counted_Object'(Pointer => (Value => Value, Count => 1) );

-- 
Jeff Carter
"Blessed are they who convert their neighbors'
oxen, for they shall inhibit their girth."
Monty Python's Life of Brian
83


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

* Re: controlled types and point of instanciation
  2018-03-16 14:39 controlled types and point of instanciation Mehdi Saada
  2018-03-16 16:31 ` J-P. Rosen
  2018-03-16 18:06 ` Mehdi Saada
@ 2018-03-17  0:54 ` Mehdi Saada
  2 siblings, 0 replies; 6+ messages in thread
From: Mehdi Saada @ 2018-03-17  0:54 UTC (permalink / raw)


I thought so ! Thanks to confirm it.


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

end of thread, other threads:[~2018-03-17  0:54 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-16 14:39 controlled types and point of instanciation Mehdi Saada
2018-03-16 16:31 ` J-P. Rosen
2018-03-16 17:14   ` Mehdi Saada
2018-03-16 18:06 ` Mehdi Saada
2018-03-16 21:44   ` Jeffrey R. Carter
2018-03-17  0:54 ` Mehdi Saada

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