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=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,b5ab7c96b188b59e X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2004-01-16 16:15:48 PST Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newspeer.monmouth.com!news.monmouth.com!shell.monmouth.com!not-for-mail From: ka@sorry.no.email (Kenneth Almquist) Newsgroups: comp.lang.ada Subject: Re: The "()" operator revisited. Date: 16 Jan 2004 19:15:40 -0500 Organization: A poorly-installed InterNetNews site Message-ID: References: <820a00puecpmodp3vg43gpd7063rr5lj8q@4ax.com> NNTP-Posting-Host: shell.monmouth.com Xref: archiver1.google.com comp.lang.ada:4475 Date: 2004-01-16T19:15:40-05:00 List-Id: Dmitry A. Kazakov wrote: > The problem is that without notion of abstract array as a class-wide > or generic parameter type, there will be no way to write > class-wide/generic subroutines taking advantage of indexing. Let I > have all containers of different kind having () indexing. This will > not help me. Because I will still be unable as little as just to write > a universal program searching max element in *a* container. You can write it. The problem is that it's awkward to instantiate. generic type Collection is limited private; type Index is (<>); type Element is private; with function "()"(C : Collection; I : Index) return Element is <>; with function First(C : Collection) return Index is <>; with function Last(C : Collection) return Index is <>; with function ">"(Left, Right : Element) return Boolean is <>; Element_First : Element; -- Value of Element'first function Max(C : Collection) return Element; function Max(C : Collection) return Element is Result : Element := First; begin for I in First(C) .. Last(C) loop if C(I) > Result then Result := C(I); end if; end loop; return Result; end Max; >> To get something that could match a generic array parameter would require >> a lot of restrictions on abstract arrays, in particular that the indice >> types would have to be abstract discrete types. > > No. We should simply go on and introduce abstract index type as a > counterpart of the formal discrete type: > > type X is (<>); > > One should be consequent. We need abstract discrete, integer, index, > numeric, string, array, access types. Have I forgot something? (:-)) The problem with extending the Ada class system to cover user defined types is that the language specification is never going to include all the classes that user defined types might fall into. Languages like Haskell get around this by allowing the programmer to define new classes. Ada already has a work-around for the lack of user-defined classes: default values for generic subprogram parameters. I that that the way to support user defined types which act like arrays is to (1) make array indexing be an operator, as was proposed at the start of this thread, (2) allow user defined types to have attributes such as 'first and 'last, and (3) allow the generic to specify that it's parameters must support specific attributes. I'd also define attributes for array types which give the index and component types, and allow generic type parameters to have defaults. With these changes, the Max generic becomes: generic type Collection is limited private; type Index is (<>) := Collection'Index_Type; type Element is private := Collection'Element_Type; with function "()"(C : Collection; I : Index) return Element is <>; with Collection'Object'First; with Collection'Object'Last; with function ">"(Left, Right : Element) return Boolean is <>; with Element'First; function Max(C : Collection) return Element; The directive "wtih Element'First;" means that type Element provides the 'First attribute. The directive "with Collection'Object'First;" means that objects of type Collection provide the 'First attribute. With these changes, the function Max can be sensibly instantiated by providing a single parameter specifying an array type. Kenneth Almquist