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=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE, MSGID_SHORT autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!samsung!uunet!mcsun!inria!enst!zeus!rosen From: rosen@zeus.enst.fr (Jean Pierre Rosen) Newsgroups: comp.lang.ada Subject: Re: constraint error question for language lawyers Summary: Funny consequence of assignment/subtyping rules... Keywords: unreasonable language restrictions frustrated programmer Message-ID: <1174@zeus.enst.fr> Date: 17 Nov 90 10:12:09 GMT References: <1802@software.software.org> Organization: Telecom-Paris, Paris France List-Id: 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"