comp.lang.ada
 help / color / mirror / Atom feed
* Ada Distilled problem
@ 2016-03-04 18:21 Simon Wright
  2016-03-04 18:58 ` Brad Moore
  2016-03-04 19:01 ` Jeffrey R. Carter
  0 siblings, 2 replies; 4+ messages in thread
From: Simon Wright @ 2016-03-04 18:21 UTC (permalink / raw)


A Stack Overflow user has been having problems[1] trying to use the
queue manager described at page 82 of Ada Distilled (2005 version)[2].

To see the problem in its essentials, the (inoperative!) spec could be

   generic
      type Element is tagged private;
   package Lists is
      type Item is new Element with private;
      type List is tagged private;

      procedure Insert (Self : in out List; I : Item'Class);

   private
      type Item is new Element with null record;
      type List is tagged null record;
   end Lists;

with a calling program

   with Lists;
   procedure Main is
      type Temp is tagged null record;
      package Temp_List is new Lists (Temp);

      FL : Temp_List.List;

      Instance : Temp := Temp'(null record);

   begin
      Temp_List.Insert (Self => FL, I  => Instance);
   end Main;

resulting in

   main.adb:11:40: expected type "Item'Class" defined at lists.ads:4,
   instance at line 4
   main.adb:11:40: found type "Temp" defined at line 3

and I can't see any way of twisting this so it can compile. And, in any
case, what is the point of "type Item"?

Has anyone managed to use the Ada Distilled design? (of course, nowadays
one would implement either directly or over predesigned containers).

[1]
http://stackoverflow.com/questions/35790004/ada-package-with-generic-tagged
[2] http://www.adaic.org/learn/materials/#ada_2005_books - first entry


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

* Re: Ada Distilled problem
  2016-03-04 18:21 Ada Distilled problem Simon Wright
@ 2016-03-04 18:58 ` Brad Moore
  2016-03-04 21:36   ` Simon Wright
  2016-03-04 19:01 ` Jeffrey R. Carter
  1 sibling, 1 reply; 4+ messages in thread
From: Brad Moore @ 2016-03-04 18:58 UTC (permalink / raw)


On Friday, March 4, 2016 at 11:21:28 AM UTC-7, Simon Wright wrote:
> A Stack Overflow user has been having problems[1] trying to use the
> queue manager described at page 82 of Ada Distilled (2005 version)[2].
> 
> To see the problem in its essentials, the (inoperative!) spec could be
> 
>    generic
>       type Element is tagged private;
>    package Lists is
>       type Item is new Element with private;
>       type List is tagged private;
> 
>       procedure Insert (Self : in out List; I : Item'Class);
> 
>    private
>       type Item is new Element with null record;
>       type List is tagged null record;
>    end Lists;
> 
> with a calling program
> 
>    with Lists;
>    procedure Main is
>       type Temp is tagged null record;
>       package Temp_List is new Lists (Temp);
> 
>       FL : Temp_List.List;
> 
>       Instance : Temp := Temp'(null record);
> 
>    begin
>       Temp_List.Insert (Self => FL, I  => Instance);
>    end Main;
> 
> resulting in
> 
>    main.adb:11:40: expected type "Item'Class" defined at lists.ads:4,
>    instance at line 4
>    main.adb:11:40: found type "Temp" defined at line 3
> 
> and I can't see any way of twisting this so it can compile. And, in any
> case, what is the point of "type Item"?

I dont see any point. Looks to me like the example needs to be corrected.

I would change the 'I' parameter of the Insert to be of type Element'Class, then this should compile.

Brad

> 
> Has anyone managed to use the Ada Distilled design? (of course, nowadays
> one would implement either directly or over predesigned containers).
> 
> [1]
> http://stackoverflow.com/questions/35790004/ada-package-with-generic-tagged
> [2] http://www.adaic.org/learn/materials/#ada_2005_books - first entry

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

* Re: Ada Distilled problem
  2016-03-04 18:21 Ada Distilled problem Simon Wright
  2016-03-04 18:58 ` Brad Moore
@ 2016-03-04 19:01 ` Jeffrey R. Carter
  1 sibling, 0 replies; 4+ messages in thread
From: Jeffrey R. Carter @ 2016-03-04 19:01 UTC (permalink / raw)


On 03/04/2016 11:21 AM, Simon Wright wrote:
> A Stack Overflow user has been having problems[1] trying to use the
> queue manager described at page 82 of Ada Distilled (2005 version)[2].
> 
> To see the problem in its essentials, the (inoperative!) spec could be
> 
>    generic
>       type Element is tagged private;
>    package Lists is
>       type Item is new Element with private;
>       type List is tagged private;
> 
>       procedure Insert (Self : in out List; I : Item'Class);
> 
>    private
>       type Item is new Element with null record;
>       type List is tagged null record;
>    end Lists;
> 
> with a calling program
> 
>    with Lists;
>    procedure Main is
>       type Temp is tagged null record;
>       package Temp_List is new Lists (Temp);
> 
>       FL : Temp_List.List;
> 
>       Instance : Temp := Temp'(null record);
> 
>    begin
>       Temp_List.Insert (Self => FL, I  => Instance);
>    end Main;
> 
> resulting in
> 
>    main.adb:11:40: expected type "Item'Class" defined at lists.ads:4,
>    instance at line 4
>    main.adb:11:40: found type "Temp" defined at line 3
> 
> and I can't see any way of twisting this so it can compile. And, in any
> case, what is the point of "type Item"?

I agree. This seems to be a case of extension for the sake of extension. Insert
should take an Element, or Element'Class if you think that buys the client
something. In either of those cases, the example should compile as is.

You could get the example to compile by changing Instance to

Instance : Temp_List.Item;

but I don't know how to explicitly initialize Instance, and using such a value
in real life is complicated by it being a private extension. The extension seems
to be "backward". One could write the pkg

>    package Lists is
>       type Element is tagged private;
>       type List    is tagged private;
>
>       procedure Insert (Self : in out List; Item : in Element'Class);
>    private
>       type Item is tagged null record;
>       type List is tagged null record;
>    end Lists;

and then declare Temp as

type Temp is new Lists.Element with ...

which works with some type conversions, but in general requiring clients to use
a container that contain indefinite types, even when only a single type is going
to be stored, seems like a poor design, and having the root of the class be a
private extension of a client-provided type, a mistake.

-- 
Jeff Carter
"The men get one hot meal a day: a bowl of steam."
Take the Money and Run
145


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

* Re: Ada Distilled problem
  2016-03-04 18:58 ` Brad Moore
@ 2016-03-04 21:36   ` Simon Wright
  0 siblings, 0 replies; 4+ messages in thread
From: Simon Wright @ 2016-03-04 21:36 UTC (permalink / raw)


Brad Moore <bmoore.ada@gmail.com> writes:

> On Friday, March 4, 2016 at 11:21:28 AM UTC-7, Simon Wright wrote:
>> A Stack Overflow user has been having problems[1] trying to use the
>> queue manager described at page 82 of Ada Distilled (2005 version)[2].
>> 
>> To see the problem in its essentials, the (inoperative!) spec could be
>> 
>>    generic
>>       type Element is tagged private;
>>    package Lists is
>>       type Item is new Element with private;
>>       type List is tagged private;
>> 
>>       procedure Insert (Self : in out List; I : Item'Class);
>> 
>>    private
>>       type Item is new Element with null record;
>>       type List is tagged null record;
>>    end Lists;
>> 
>> with a calling program
>> 
>>    with Lists;
>>    procedure Main is
>>       type Temp is tagged null record;
>>       package Temp_List is new Lists (Temp);
>> 
>>       FL : Temp_List.List;
>> 
>>       Instance : Temp := Temp'(null record);
>> 
>>    begin
>>       Temp_List.Insert (Self => FL, I  => Instance);
>>    end Main;
>> 
>> resulting in
>> 
>>    main.adb:11:40: expected type "Item'Class" defined at lists.ads:4,
>>    instance at line 4
>>    main.adb:11:40: found type "Temp" defined at line 3
>> 
>> and I can't see any way of twisting this so it can compile. And, in any
>> case, what is the point of "type Item"?
>
> I dont see any point. Looks to me like the example needs to be
> corrected.
>
> I would change the 'I' parameter of the Insert to be of type
> Element'Class, then this should compile.

Yes, I agree.

When I said I "can't see any way of twisting this so it can compile" I
meant that I was trying to get Main to compile without changing
Lists.


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

end of thread, other threads:[~2016-03-04 21:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-03-04 18:21 Ada Distilled problem Simon Wright
2016-03-04 18:58 ` Brad Moore
2016-03-04 21:36   ` Simon Wright
2016-03-04 19:01 ` Jeffrey R. Carter

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