From: Maciej Sobczak on 23 Jun 2010 03:30 Consider this: S : String := "Hello"; It is possible to iterate over all indices of this array with this construct: for I in S'Range loop ... I would like to declare I as a free variable instead and I would expect some symmetry in the language by doing this: I : S'Range := S'First; But it does not work. Of course, one can do: I : Positive := S'First; But it has the disadvantage of hardcoding the index type and introducing unnecessary coupling in the code. (hint: the problem is not really about Strings, it is a general array question) I have two questions: 1. What is the standard justification for this assymetry? What exactly makes S'Range "work" in a for loop? 2. Is it possible to declare the index variable without hardcoding the index type (that is, to infer it from the array object)? -- Maciej Sobczak * http://www.inspirel.com YAMI4 - Messaging Solution for Distributed Systems http://www.inspirel.com/yami4
From: Dmitry A. Kazakov on 23 Jun 2010 04:01 On Wed, 23 Jun 2010 00:30:23 -0700 (PDT), Maciej Sobczak wrote: > Consider this: > > S : String := "Hello"; > > It is possible to iterate over all indices of this array with this > construct: > > for I in S'Range loop ... > > I would like to declare I as a free variable instead and I would > expect some symmetry in the language by doing this: > > I : S'Range := S'First; subtype Index_Span is Integer range S'Range; I : Index_Span := S'First; > I have two questions: > > 1. What is the standard justification for this assymetry? "We don't want to do anything [useful]." (:-)) > What exactly > makes S'Range "work" in a for loop? It is hard wired, since ranges are not first class citizens. > 2. Is it possible to declare the index variable without hardcoding the > index type (that is, to infer it from the array object)? No, without improving the type system. E.g. introducing abstract index types, and abstract range types (or more general sets of index types), and abstract composite types like arrays. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de
From: J-P. Rosen on 23 Jun 2010 05:03 Dmitry A. Kazakov a �crit : >> I would like to declare I as a free variable instead and I would >> expect some symmetry in the language by doing this: >> >> I : S'Range := S'First; > > subtype Index_Span is Integer range S'Range; > I : Index_Span := S'First; Or simply: I : Positive range S'Range; >> 1. What is the standard justification for this assymetry? This is a simplification of loops, to avoid some typing to the user. Think of it the other way round: since a for loop involves the declaration of an object, the type should always be explicitely declared (a coding rule that I apply - of course checkable by AdaControl): for I in positive range 1..10 loop ... for I in positive range S'Range loop ... So it is really symetrical, except that in the case of a loop, you are allowed to "simplify" by omitting the "<type> range" part - forcing the compiler to deduce the type from the range, which is not a good idea IMHO. >> 2. Is it possible to declare the index variable without hardcoding the >> index type (that is, to infer it from the array object)? > There is no point in doing this, since the type of the array /is/ hardcoded anyway. You could allow the same "simplification" as for loops, but as mentionned above, I prefer the other way round: always explicitely give the type name. -- --------------------------------------------------------- J-P. Rosen (rosen(a)adalog.fr) Visit Adalog's web site at http://www.adalog.fr
From: Niklas Holsti on 23 Jun 2010 08:13 Dmitry A. Kazakov wrote: > On Wed, 23 Jun 2010 00:30:23 -0700 (PDT), Maciej Sobczak wrote: > >> Consider this: >> >> S : String := "Hello"; >> >> It is possible to iterate over all indices of this array with this >> construct: >> >> for I in S'Range loop ... >> >> I would like to declare I as a free variable instead and I would >> expect some symmetry in the language by doing this: >> >> I : S'Range := S'First; > > subtype Index_Span is Integer range S'Range; > I : Index_Span := S'First; > >> I have two questions: >> >> 1. What is the standard justification for this assymetry? > > "We don't want to do anything [useful]." (:-)) > >> What exactly >> makes S'Range "work" in a for loop? > > It is hard wired, since ranges are not first class citizens. > >> 2. Is it possible to declare the index variable without hardcoding the >> index type (that is, to infer it from the array object)? > > No, without improving the type system. E.g. introducing abstract index > types, and abstract range types (or more general sets of index types), and > abstract composite types like arrays. I don't think such large language changes would be necessary. The expression S'Range gives the compiler all the information about the type and its constraints, so I see no reason why Ada could not be extended simply to allow S'Range in a variable declaration, as in the above quoted "I : S'Range". The declared variable "I" would have the same (sub)type as the loop counter in "for I in S'Range loop ...". On the other hand, I haven't really felt the need for this extension in my programs. Whenever I declare an array type, either the index type has no meaning for the application, and I use a subtype of Integer (eg. the array just represents a sequence or set of objects), or I also define an index type (eg. an object identifier) that is associated with the array type. So I have not felt a need for using S'Range in a variabnle declaration. -- Niklas Holsti Tidorum Ltd niklas holsti tidorum fi . @ .
From: Georg Bauhaus on 23 Jun 2010 08:24
On 23.06.10 11:03, J-P. Rosen wrote: >>> 2. Is it possible to declare the index variable without hardcoding the >>> index type (that is, to infer it from the array object)? >> > There is no point in doing this, since the type of the array /is/ > hardcoded anyway. You could allow the same "simplification" as for > loops, but as mentionned above, I prefer the other way round: always > explicitely give the type name. Is the required name somehow related to why the Ada.Container generics do not re-export the formal types? But anyway, without Dmitry's abstractions, a partial solution should work along these lines, I think, generic type Index_Type is (<>); type Component_Type is private; package GA is type Array_Type is array (Index_Type range <>) of Component_Type; generic with function "+" (Left, Right: Component_Type) return Component_Type is <>; Zero: in Component_Type; package Algorithms is function Sum (A: Array_Type) return Component_Type ; end Algorithms; end GA; |