comp.lang.ada
 help / color / mirror / Atom feed
* specifying only 'First of an index in an array
@ 2021-02-03 17:47 Mehdi Saada
  2021-02-03 19:29 ` Jeffrey R. Carter
  0 siblings, 1 reply; 5+ messages in thread
From: Mehdi Saada @ 2021-02-03 17:47 UTC (permalink / raw)


Is there a way, on nominal or genetic array type definition (I mean in generic specifications), to ensure that Index_type'First is always the same, but that arrays can still grow ?

something like (certainly wrong): type my_type is array (Scalar_type range scalar_type'first..<>) ?

That or I suppose I can wrap a function around that type and make it private to avoid range incompatibilities...

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: specifying only 'First of an index in an array
  2021-02-03 17:47 specifying only 'First of an index in an array Mehdi Saada
@ 2021-02-03 19:29 ` Jeffrey R. Carter
  2021-02-03 21:06   ` Mehdi Saada
  0 siblings, 1 reply; 5+ messages in thread
From: Jeffrey R. Carter @ 2021-02-03 19:29 UTC (permalink / raw)


On 2/3/21 6:47 PM, Mehdi Saada wrote:
> Is there a way, on nominal or genetic array type definition (I mean in generic specifications), to ensure that Index_type'First is always the same, but that arrays can still grow ?

Arrays can't grow in Ada, so I don't know what you're asking. For a sequence 
with a fixed lower bound and variable length you can use Ada.Containers.Vectors.

-- 
Jeff Carter
"Sheriff murdered, crops burned, stores looted,
people stampeded, and cattle raped."
Blazing Saddles
35

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: specifying only 'First of an index in an array
  2021-02-03 19:29 ` Jeffrey R. Carter
@ 2021-02-03 21:06   ` Mehdi Saada
  2021-02-03 21:45     ` Jeffrey R. Carter
  0 siblings, 1 reply; 5+ messages in thread
From: Mehdi Saada @ 2021-02-03 21:06 UTC (permalink / raw)


I mean the possibility of specifying a minimal bound for index values, but not the outer bound.
So still an array TYPE declaration constrained for one bound and not the other. And constrained fully at instanciation of course.
But now given an array of which I don't know the exact 'first I do all kinds of t_index'val(T_index'pos(Object'first) + t_index'pos(increment)), and I don't need thinking much about ranges anymore.
the more generic the better.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: specifying only 'First of an index in an array
  2021-02-03 21:06   ` Mehdi Saada
@ 2021-02-03 21:45     ` Jeffrey R. Carter
  2021-02-03 22:15       ` Mehdi Saada
  0 siblings, 1 reply; 5+ messages in thread
From: Jeffrey R. Carter @ 2021-02-03 21:45 UTC (permalink / raw)


On 2/3/21 10:06 PM, Mehdi Saada wrote:
> I mean the possibility of specifying a minimal bound for index values, but not the outer bound.
> So still an array TYPE declaration constrained for one bound and not the other. And constrained fully at instanciation of course.

This was discussed here recently referring specifically to strings.

Since these are sequences, the index should be numeric with a lower bound of 1.

Ada has had a way to do this since Ada 83:

type T_Base is array (Positive range <>) of Element;

type T (Length : Natural) is record
    Value : T_Base (1 .. Length);
end record;

Ada 12 also adds the possibility of

subtype T is T_Base with
    Dynamic_Predicate => T'First = 1;

There is also the possibility of using a Vector for this.

The record has the advantage that sliding works, and the disadvantage that you 
have to put .Value in a lot of places.

The predicate has the advantage that it is an array type and objects can be 
indexed directly, and the disadvantage that sliding doesn't work.

Vectors have the advantage that the length can vary, and the disadvantages that 
slicing doesn't exist and conversions between Vector and T_Base are more complex 
than for the other forms.

-- 
Jeff Carter
"Sheriff murdered, crops burned, stores looted,
people stampeded, and cattle raped."
Blazing Saddles
35

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: specifying only 'First of an index in an array
  2021-02-03 21:45     ` Jeffrey R. Carter
@ 2021-02-03 22:15       ` Mehdi Saada
  0 siblings, 0 replies; 5+ messages in thread
From: Mehdi Saada @ 2021-02-03 22:15 UTC (permalink / raw)


This was definitive and clear, and took a prominent place in my course documents, a thousand thanks !

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-02-03 22:15 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-03 17:47 specifying only 'First of an index in an array Mehdi Saada
2021-02-03 19:29 ` Jeffrey R. Carter
2021-02-03 21:06   ` Mehdi Saada
2021-02-03 21:45     ` Jeffrey R. Carter
2021-02-03 22:15       ` Mehdi Saada

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox