comp.lang.ada
 help / color / mirror / Atom feed
* How can I create an empty array of a generic type?
@ 2019-07-13  3:36 b.mcguinness747
  2019-07-13  4:11 ` Keith Thompson
  2019-07-13 21:02 ` b.mcguinness747
  0 siblings, 2 replies; 12+ messages in thread
From: b.mcguinness747 @ 2019-07-13  3:36 UTC (permalink / raw)


I have a generic package


generic
  type Element_Type is private;
  type Key_Component_Type is private;
  with function "<" (Left, Right : Key_Component_Type) return Boolean is <>;
  with function "=" (Left, Right : Element_Type) return Boolean is <>;
package Ternary_Trees is

...


private

...

  Empty_Key  : constant Key_Type(1..0) := (others => Key_Component_Type'First);

...

end Ternary_Trees;


When I try to compile the package, I get an error message:

ternary_trees.ads:77:54: prefix for "First" attribute may not be private type


How can I make this work?  I want to create an empty array so I can build up a key through concatenation as I descend recursively through a tree.

Thanks.

--- Brian McGuinness


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

* Re: How can I create an empty array of a generic type?
  2019-07-13  3:36 How can I create an empty array of a generic type? b.mcguinness747
@ 2019-07-13  4:11 ` Keith Thompson
  2019-07-13  5:24   ` J-P. Rosen
  2019-07-13 21:02 ` b.mcguinness747
  1 sibling, 1 reply; 12+ messages in thread
From: Keith Thompson @ 2019-07-13  4:11 UTC (permalink / raw)


b.mcguinness747@gmail.com writes:
> I have a generic package
>
> generic
>   type Element_Type is private;
>   type Key_Component_Type is private;
>   with function "<" (Left, Right : Key_Component_Type) return Boolean is <>;
>   with function "=" (Left, Right : Element_Type) return Boolean is <>;
> package Ternary_Trees is
> ...
> private
> ...
>   Empty_Key  : constant Key_Type(1..0) := (others => Key_Component_Type'First);
> ...
> end Ternary_Trees;
>
> When I try to compile the package, I get an error message:
>
> ternary_trees.ads:77:54: prefix for "First" attribute may not be private type

You haven't shown us the declaration for Key_Type.  Presumably it's an
array of Key_Component_Type.

This compiles:

generic
  type Element_Type is private;
  type Key_Component_Type is private;
  with function "<" (Left, Right : Key_Component_Type) return Boolean is <>;
  with function "=" (Left, Right : Element_Type) return Boolean is <>;
package Ternary_Trees is
  type Key_Type is array(Positive range <>) of Key_Component_Type;
private
  Dummy: Key_Component_Type;
  Empty_Key  : constant Key_Type(1..0) := (others => Dummy);
end Ternary_Trees;

The Dummy value is a bit ugly.  An alternative is to require the
instantiator to provide a value:

generic
  type Element_Type is private;
  type Key_Component_Type is private;
  Default_Key_Component: Key_Component_Type;
  with function "<" (Left, Right : Key_Component_Type) return Boolean is <>;
  with function "=" (Left, Right : Element_Type) return Boolean is <>;
package Ternary_Trees is
  type Key_Type is array(Positive range <>) of Key_Component_Type;
private
  Dummy: Key_Component_Type;
  Empty_Key  : constant Key_Type(1..0) := (others => Default_Key_Component);
end Ternary_Trees;

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

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

* Re: How can I create an empty array of a generic type?
  2019-07-13  4:11 ` Keith Thompson
@ 2019-07-13  5:24   ` J-P. Rosen
  2019-07-13 21:19     ` Keith Thompson
  2019-07-14 11:15     ` reinert
  0 siblings, 2 replies; 12+ messages in thread
From: J-P. Rosen @ 2019-07-13  5:24 UTC (permalink / raw)


Le 13/07/2019 à 06:11, Keith Thompson a écrit :
> generic
>   type Element_Type is private;
>   type Key_Component_Type is private;
>   with function "<" (Left, Right : Key_Component_Type) return Boolean is <>;
>   with function "=" (Left, Right : Element_Type) return Boolean is <>;
> package Ternary_Trees is
>   type Key_Type is array(Positive range <>) of Key_Component_Type;
> private
>   Dummy: Key_Component_Type;
>   Empty_Key  : constant Key_Type(1..0) := (others => Dummy);
> end Ternary_Trees;
> 
> The Dummy value is a bit ugly. 
You can avoid it:
   Empty_Key  : constant Key_Type(1..0) := (others => <>);

-- 
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] 12+ messages in thread

* Re: How can I create an empty array of a generic type?
  2019-07-13  3:36 How can I create an empty array of a generic type? b.mcguinness747
  2019-07-13  4:11 ` Keith Thompson
@ 2019-07-13 21:02 ` b.mcguinness747
  2019-07-14 16:41   ` bj.mooremr
  1 sibling, 1 reply; 12+ messages in thread
From: b.mcguinness747 @ 2019-07-13 21:02 UTC (permalink / raw)


On Friday, July 12, 2019 at 11:36:02 PM UTC-4, b.mcgui...@gmail.com wrote:
> I have a generic package
> 
> 
> generic
>   type Element_Type is private;
>   type Key_Component_Type is private;
>   with function "<" (Left, Right : Key_Component_Type) return Boolean is <>;
>   with function "=" (Left, Right : Element_Type) return Boolean is <>;
> package Ternary_Trees is
> 
> ...
> 
> 
> private
> 
> ...
> 
>   Empty_Key  : constant Key_Type(1..0) := (others => Key_Component_Type'First);
> 
> ...
> 
> end Ternary_Trees;
> 
> 
> When I try to compile the package, I get an error message:
> 
> ternary_trees.ads:77:54: prefix for "First" attribute may not be private type
> 
> 
> How can I make this work?  I want to create an empty array so I can build up a key through concatenation as I descend recursively through a tree.
> 
> Thanks.
> 
> --- Brian McGuinness

Ok.  Thanks for the help

--- Brian McGuinness


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

* Re: How can I create an empty array of a generic type?
  2019-07-13  5:24   ` J-P. Rosen
@ 2019-07-13 21:19     ` Keith Thompson
  2019-07-14 11:09       ` reinert
  2019-07-14 11:15     ` reinert
  1 sibling, 1 reply; 12+ messages in thread
From: Keith Thompson @ 2019-07-13 21:19 UTC (permalink / raw)


"J-P. Rosen" <rosen@adalog.fr> writes:
> Le 13/07/2019 à 06:11, Keith Thompson a écrit:
>> generic
>>   type Element_Type is private;
>>   type Key_Component_Type is private;
>>   with function "<" (Left, Right : Key_Component_Type) return Boolean is <>;
>>   with function "=" (Left, Right : Element_Type) return Boolean is <>;
>> package Ternary_Trees is
>>   type Key_Type is array(Positive range <>) of Key_Component_Type;
>> private
>>   Dummy: Key_Component_Type;
>>   Empty_Key  : constant Key_Type(1..0) := (others => Dummy);
>> end Ternary_Trees;
>> 
>> The Dummy value is a bit ugly. 
> You can avoid it:
>    Empty_Key  : constant Key_Type(1..0) := (others => <>);

Yes, that's better than my suggestion.  (That feature was introduced in
Ada 2005.)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */


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

* Re: How can I create an empty array of a generic type?
  2019-07-13 21:19     ` Keith Thompson
@ 2019-07-14 11:09       ` reinert
  0 siblings, 0 replies; 12+ messages in thread
From: reinert @ 2019-07-14 11:09 UTC (permalink / raw)


lørdag 13. juli 2019 23.19.28 UTC+2 skrev Keith Thompson følgende:
> "J-P. Rosen" <rosen@adalog.fr> writes:
> > Le 13/07/2019 à 06:11, Keith Thompson a écrit:
> >> generic
> >>   type Element_Type is private;
> >>   type Key_Component_Type is private;
> >>   with function "<" (Left, Right : Key_Component_Type) return Boolean is <>;
> >>   with function "=" (Left, Right : Element_Type) return Boolean is <>;
> >> package Ternary_Trees is
> >>   type Key_Type is array(Positive range <>) of Key_Component_Type;
> >> private
> >>   Dummy: Key_Component_Type;
> >>   Empty_Key  : constant Key_Type(1..0) := (others => Dummy);
> >> end Ternary_Trees;
> >> 
> >> The Dummy value is a bit ugly. 
> > You can avoid it:
> >    Empty_Key  : constant Key_Type(1..0) := (others => <>);
> 
> Yes, that's better than my suggestion.  (That feature was introduced in
> Ada 2005.)
> 
> -- 
> Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
> Will write code for food.
> void Void(void) { Void(); } /* The recursive call of the void */

But Key_Type is an array indexed by Positive (> 0).  
The zero in "Key_Type(1..0)" is not so intuitive for me (outside the range) ?

reinert

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

* Re: How can I create an empty array of a generic type?
  2019-07-13  5:24   ` J-P. Rosen
  2019-07-13 21:19     ` Keith Thompson
@ 2019-07-14 11:15     ` reinert
  2019-07-14 11:27       ` Jeffrey R. Carter
  2019-07-14 12:49       ` J-P. Rosen
  1 sibling, 2 replies; 12+ messages in thread
From: reinert @ 2019-07-14 11:15 UTC (permalink / raw)


lørdag 13. juli 2019 07.24.10 UTC+2 skrev J-P. Rosen følgende:
.....
> You can avoid it:
>    Empty_Key  : constant Key_Type(1..0) := (others => <>);
> 

But Key_Type is an array indexed by Positive (> 0).  
The zero in "Key_Type(1..0)" is not so intuitive for me (outside the range) ?

reinert

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

* Re: How can I create an empty array of a generic type?
  2019-07-14 11:15     ` reinert
@ 2019-07-14 11:27       ` Jeffrey R. Carter
  2019-07-14 12:49       ` J-P. Rosen
  1 sibling, 0 replies; 12+ messages in thread
From: Jeffrey R. Carter @ 2019-07-14 11:27 UTC (permalink / raw)


On 7/14/19 1:15 PM, reinert wrote:
> lørdag 13. juli 2019 07.24.10 UTC+2 skrev J-P. Rosen følgende:
> .....
>> You can avoid it:
>>     Empty_Key  : constant Key_Type(1..0) := (others => <>);
>>
> 
> But Key_Type is an array indexed by Positive (> 0).
> The zero in "Key_Type(1..0)" is not so intuitive for me (outside the range) ?

Try this:

with Ada.Text_IO;

procedure Null_Last is
    S : constant String := "";
begin -- Null_Last
    Ada.Text_IO.Put_Line (Item => Integer'Image (S'Last) );
end Null_Last;

See also ARM 3.5:

(4) "A range with lower bound L and upper bound R is described by “L .. R”. If R 
is less than L, then the range is a null range, and specifies an empty set of 
values."

(8) "A range is compatible with a scalar subtype if and only if it is either a 
null range or each bound of the range belongs to the range of the subtype."

So 1 .. 0 is a null range. A null range is compatible with any scalar subtype, 
so it is compatible with Positive.

-- 
Jeff Carter
"I have a very small head and I had
better learn to live with it ..."
Edsger Dijkstra
158

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

* Re: How can I create an empty array of a generic type?
  2019-07-14 11:15     ` reinert
  2019-07-14 11:27       ` Jeffrey R. Carter
@ 2019-07-14 12:49       ` J-P. Rosen
  1 sibling, 0 replies; 12+ messages in thread
From: J-P. Rosen @ 2019-07-14 12:49 UTC (permalink / raw)


Le 14/07/2019 à 13:15, reinert a écrit :
> lørdag 13. juli 2019 07.24.10 UTC+2 skrev J-P. Rosen følgende:
> .....
>> You can avoid it:
>>    Empty_Key  : constant Key_Type(1..0) := (others => <>);
>>
> 
> But Key_Type is an array indexed by Positive (> 0).  
> The zero in "Key_Type(1..0)" is not so intuitive for me (outside the range) ?
> 
> reinert
> 
A null range is always compatible, even if the bounds do not belong to
the subtype. 3.5(8).

-- 
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] 12+ messages in thread

* Re: How can I create an empty array of a generic type?
  2019-07-13 21:02 ` b.mcguinness747
@ 2019-07-14 16:41   ` bj.mooremr
  2019-10-16  2:03     ` Andrew Shvets
  0 siblings, 1 reply; 12+ messages in thread
From: bj.mooremr @ 2019-07-14 16:41 UTC (permalink / raw)


With Ada 2020, you'll also be able to write the following, which eliminates the need for specifying a null range;

Empty_Key  : constant Key_Type := [];

Brad


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

* Re: How can I create an empty array of a generic type?
  2019-07-14 16:41   ` bj.mooremr
@ 2019-10-16  2:03     ` Andrew Shvets
  2019-10-16  4:49       ` Egil H H
  0 siblings, 1 reply; 12+ messages in thread
From: Andrew Shvets @ 2019-10-16  2:03 UTC (permalink / raw)


On Sunday, July 14, 2019 at 12:41:41 PM UTC-4, bj.m...@gmail.com wrote:
> With Ada 2020, you'll also be able to write the following, which eliminates the need for specifying a null range;
> 
> Empty_Key  : constant Key_Type := [];
> 
> Brad

Where did you see that in the ARM 202X?

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

* Re: How can I create an empty array of a generic type?
  2019-10-16  2:03     ` Andrew Shvets
@ 2019-10-16  4:49       ` Egil H H
  0 siblings, 0 replies; 12+ messages in thread
From: Egil H H @ 2019-10-16  4:49 UTC (permalink / raw)


On Wednesday, October 16, 2019 at 4:03:40 AM UTC+2, Andrew Shvets wrote:
> On Sunday, July 14, 2019 at 12:41:41 PM UTC-4, bj.m...@gmail.com wrote:
> > With Ada 2020, you'll also be able to write the following, which eliminates the need for specifying a null range;
> > 
> > Empty_Key  : constant Key_Type := [];
> > 
> > Brad
> 
> Where did you see that in the ARM 202X?


For arrays, RM 4.3.3.3 (3.1/5):
null_array_aggregate ::= '[' ']'

http://ada-auth.org/standards/2xrm/html/RM-4-3-3.html


For containers, RM 4.3.5 (14/5):
null_container_aggregate ::= '[' ']'

http://ada-auth.org/standards/2xrm/html/RM-4-3-5.html

-- 
~egilhh


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

end of thread, other threads:[~2019-10-16  4:49 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-13  3:36 How can I create an empty array of a generic type? b.mcguinness747
2019-07-13  4:11 ` Keith Thompson
2019-07-13  5:24   ` J-P. Rosen
2019-07-13 21:19     ` Keith Thompson
2019-07-14 11:09       ` reinert
2019-07-14 11:15     ` reinert
2019-07-14 11:27       ` Jeffrey R. Carter
2019-07-14 12:49       ` J-P. Rosen
2019-07-13 21:02 ` b.mcguinness747
2019-07-14 16:41   ` bj.mooremr
2019-10-16  2:03     ` Andrew Shvets
2019-10-16  4:49       ` Egil H H

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