comp.lang.ada
 help / color / mirror / Atom feed
From: rosen@zeus.enst.fr (Jean Pierre Rosen)
Subject: Re: constraint error question for language lawyers
Date: 17 Nov 90 10:12:09 GMT	[thread overview]
Message-ID: <1174@zeus.enst.fr> (raw)
In-Reply-To: 1802@software.software.org

In article <1802@software.software.org>, blakemor@software.org (Alex Blakemore) writes:
> OK, language lawyers of the world.
> This one has stumped everyone I've asked locally,  including several 
> people who have worked with Ada since there were compilers for it.
> ... 
>   type short is range 0 .. 10;
> 
>   type data is array (short range <>) of character;
> 
>   type var_text (len : short := 0) is
>     record
>       text : data (1 .. len);
>     end record;
> 
>   dummy := (len  => dummy.len + 1,
>             text => dummy.text & 'a');
> 
OKAY, the question is "what is the subtype (i.e. bounds) of dummy.text?
4.5.3(4) says that if the left-hand part of a catenation operator is a 
null array (as in this case), the lower bound is taken from the right operand
4.5.3(5) says that if the operant is of a component type, its lower bound is
taken from the index subtype. Therefore, 'a' is considered of subtype
data(0..0). BUT TEXT EXPECTS SUBTYPE data(1..1)! And array sliding does
NOT occur within aggregates (sliding occurs only in array assignments, 5.2.1)
=> CONSTRAINT_ERROR.
Clean way of solving the problem:
type extended_index is range 0..10;
subtype index is extended_index range 1..extended_index'LAST;
type data is array(index range <>) of character;
 
You might complain about strange language rules, but note that this is
actually a design error: you must express that the 0 is not a suitable value
for indexing your array. Once you express your design carefully, the problem
disappears.

"ADA DOES NOT CREATE PROBLEMS; IT JUST MAKES HIDDEN PROBLEMS APPARENT"

  parent reply	other threads:[~1990-11-17 10:12 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1990-11-08 17:18 constraint error question for language lawyers Alex Blakemore
1990-11-09 14:12 ` David Collard
1990-11-10  1:08 ` Charles H. Sampson
1990-11-17 10:12 ` Jean Pierre Rosen [this message]
  -- strict thread matches above, loose matches on Subject: below --
1990-11-09 14:36 "Norman H. Cohen"
1990-11-30 16:02 ` James THIELE
replies disabled

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