From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.ada Subject: Re: How can I create an empty array of a generic type? Date: Fri, 12 Jul 2019 21:11:13 -0700 Organization: None to speak of Message-ID: References: <5f53e122-6b99-48ca-b6cc-4ae43c0a2221@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="494b121aa0c8f1d94b8aec7426cc7869"; logging-data="5825"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+QGTslBp6Yoss59dnYI7l6" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:HNU6IKYD6/FW22/BU6olGmo644U= sha1:SDhd4qeJRIgIwyfyt++De0xmYbs= Xref: reader01.eternal-september.org comp.lang.ada:56862 Date: 2019-07-12T21:11:13-07:00 List-Id: 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 Will write code for food. void Void(void) { Void(); } /* The recursive call of the void */