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!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Natasha Kerensikova Newsgroups: comp.lang.ada Subject: Re: How to declare a generic formal type "covered" by another? Date: Thu, 1 May 2014 07:33:19 +0000 (UTC) Organization: A noiseless patient Spider Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Date: Thu, 1 May 2014 07:33:19 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="76a49b86bc3e16725b7cfca3d85cb4c8"; logging-data="31043"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX182AyvMlBjQY1w+aeIe7cta" User-Agent: slrn/1.0.1 (FreeBSD) Cancel-Lock: sha1:M9vGhbcCdX8FkRCNLlcO4pWM2dk= Xref: news.eternal-september.org comp.lang.ada:19631 Date: 2014-05-01T07:33:19+00:00 List-Id: Hello, On 2014-05-01, J-P. Rosen wrote: > Le 01/05/2014 08:54, Natasha Kerensikova a écrit : >> So S would have to be a concrete constrained type, but looking at the >> alternatives for "formal_type_definition", I can't see anything that >> covers both S being a constrained version of T, and S being a concrete >> tagged type belonging to class-wide T. >> >> Is it possible, or am I hitting a limit of Ada generics? > > 1) There is nothing here that requires T to be tagged Indeed, and I don't want to restrict T to tagged types. The examples I had in mind are: - T is Stream_Element_Array, S is Stream_Element_Array (1 .. 4096) - T is Root_Stream_Type'Class, S is a concrete type derived from it > 2) You can say that S must be derived from T: > type S is new T; (non tagged type) > type S is new T with private; (tagged type) It looks like I can't cover both cases in a single declaration, can I? > 3) There is no way to say that S must be constrained, because being > constrained is a matter of subtype, and only types matter for generics. Well maybe the problem here is my limited vocabulary. I used the word "constrained" there only to mean "Something := new S" or "Variable : S;" are both valid, i.e. objects of that type can be created without further information. There must be some way to express that in generics. > The best way to constrain an allocator is by providing an initial value, > that you could pass as a parameter to Allocate f.e. That's the solution I have been running with, but yeasterday I hit a problem with T being Stream_Element_Array and S being Stream_Element_Array (1 .. 2**29): the intermediate copy and the allocator result don't fit both in memory, and the program is OOM killed. The only solution I found where a limited discriminated record to hold the Stream_Element_Array, but that's an invasive changed, and somehow having the allocator run before filling its contents, which means either exposing the internal access type (so that a client function can call the allocator and return the access), or providing a generic function like in the OP (or some other solution I'm unable to come up with). Thanks for your help, Natasha