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!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Musing on defining attributes and the ability to define an "abstract type X"-interface. Date: Thu, 20 Jul 2017 09:52:11 +0200 Organization: Aioe.org NNTP Server Message-ID: References: <9617c73b-e23e-405b-8544-4d17e7e3ad61@googlegroups.com> NNTP-Posting-Host: vZYCW951TbFitc4GdEwQJg.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 X-Notice: Filtered by postfilter v. 0.8.2 Content-Language: en-US Xref: news.eternal-september.org comp.lang.ada:47458 Date: 2017-07-20T09:52:11+02:00 List-Id: On 20/07/2017 02:06, Shark8 wrote: > Also, somewhat tangential to my post on user-defined attributes, if > there were a way to define attributes could it perhaps be used to make > an "abstract type" interface, allowing something like: > > Abstract Type Array_Type is record > -- Not predefined attributes. > Function Index_Count return Positive; > Function Index_Type( Index : Positive := 1 ) return (<>); -- As in generic parameters. > > -- Predefined Array Attributes. > Function First ( Index : Positive := 1 ) return Index_Type(Index) > with Pre => Index <= Array_Type'Index_Count; > Function Last ( Index : Positive := 1 ) return Index_Type(Index) > with Pre => Index <= Array_Type'Index_Count; > Function Length ( Index : Positive := 1 ) return Natural > is ( Integer'Succ(Array_Type'Last(Index) - Array_Type'First(Index)) ); > Function Range ( Index : Positive := 1 ) return Range -- Not in current Ada. > is ( Array_Type'First(Index)..Array_Type'Last(Index) ); > > -- Predefined Subtype Attributes. > Function Alignment return Universal_Integer > with Post => Alignment'Result >= 0; > Function Definite return Boolean; > > end record; > > And, of course my example brings up points that would need to be addressed: > (1) How would we indicate that Array_Type has a non-null ordered set > of indexers. [ Array_Type's Range attribute {1..X | x is the > dimensionality of the array} ] You need along with the array type: 1. Index type 2. Index range type (or general index set type) 3. Array element type 4. Array set of elements type This is for 1D arrays. For ND arrays you need types in-between (tuples of indices) and sub-arrays. > (2) How would we indicate the [sub]types of the indexes are not necessarily of the same [sub]type? [ex. Array(Integer,character) of X ] > (3) How would we indicate the "constrainedness" of a particular index? (Or does this matter?) [ex Array(Integer range <>,character) of X ] > (4) It seems ranges would have to become first-class types in order > to satisfy the return-value of the Range attribute. (Though perhaps > some compiler-magic like is done w/ Ada.Iterator_Interfaces and the > for-loop might handle that...) Yes. And you need index operations defined on non-flattened indices and sets of. Compare: A (I, J) -- Flatten A ((I, J)) -- Elevated, same as Point := (I, J); Element := A (Point); > (5) It is uncertain whether making these inheritable would be good: > on the plus side you could define an abstract-type that held the READ > and WRITE and other common attributes and have a hierarchy of > "type-classes". It is an interesting question which was discussed in OO. E.g. if an array of subtypes is a subtype (a subtype in general terms, rather than Ada's sense). In some cases this is required, e.g. if we tried to bring string types in order. Surely ASCII arrays must be compatible with Latin-1 array etc. > (6) The exposure of heretofore conceptual types. (e.g. Universal_Integer) Array requires positional access, just like discrete types have T'Pos and T'Val. At leas for iteration, it does. > (7) The exposure of the "type of a type". This is difficult to make consistent with inheritance and dynamic dispatch. Dispatching tables tend to be global. With type types you need tagged types come and go. Difficult. > (8) Would it be worth it for compiler-writers? Language maintainers? > Language users? -- On the one hand being able to indicate explicitly > that "definite type X" has *these* attributes while "Fixed-point type Y" > has *those* attributes might be pretty nice... especially if we could > make interfaces a bit more consistant. (e.g. allowing both > Indefinite_Vector_Var'Length and Array_Var'Length.) Clearly an attribute is just a primitive operation. There cannot be any discussion about it. > (9) On the user-side I think it would be great to be able to have > the Ada.Containers.Vector Vector-type have a compatible interface to > Array... we're halfway there with the Vector_Var'Iterate + For-loop anyway. Yes, having non-generic containers would be a huge relief. But you know the answer already, don't you? (:-)) -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de