From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Path: eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: "unconstrained subtype in component declaration" vs "discriminant in constraint must appear alone" Date: Wed, 24 Feb 2021 11:04:58 +0100 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: <4086f350-c56b-4eec-91d3-ef685230b011n@googlegroups.com> <67ea71d9-489a-4802-9cf0-d955f6ecce62n@googlegroups.com> <0e88c615-6a53-4392-b83d-581c59473c88n@googlegroups.com> <6716a4e5-02ac-4735-83c9-28bff3f8aae6n@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 24 Feb 2021 10:04:58 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="d884e09fcd2ae1adf47437814efa1a7e"; logging-data="27555"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Y9omRxONHsFHRGif9JQDDHI8y/iKtLl0=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 Cancel-Lock: sha1:opRbWjbFkz18btlyhH1CT6noBAg= In-Reply-To: <6716a4e5-02ac-4735-83c9-28bff3f8aae6n@googlegroups.com> Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:61432 List-Id: On 2/23/21 11:06 PM, Mehdi Saada wrote: > > type CARDS is new cards_numbers range 1..52; > --> > 41:10 missing case values: -128 .. 0 > 41:10 missing case values: 5 .. 127 > 41:25 subtype of expression is not static, alternatives must cover base type This tells you why. > from --> > [card in CARDS, of course] > case (card / 13) +1 is > when 1 => put("Spades"); > when 2 =>put("Hearts"); > when 3 =>put("Spades"); > when 4 =>put("Clovers"); > end case; > > how can the compiler not know that CARD is limited from 1 to 52, including 0 for the base type (type Cards_numbers is range 0..52) You have "Spades" twice. It would be clearer and less error-prone to have when 1 | 3 => Put ("Spades"); if that's what you mean. The compiler knows the subtype of Card, but since you have a signed type, the base type is roughly symmetrical around zero. The operators take and return values of the base type, so the compiler assumes the expression may have any value of the base type. What you have is roughly equivalent to type Cards_Numbers'Base is range /implementation_defined/; -- H/W type that includes 0 .. 52 subtype Cards_Numbers is Cards_Numbers'Base range 0 .. 52; type Cards'Base is new Cards_Numbers'Base; subtype Cards is Cards'Base range 1 .. 52; Note also that 52 / 13 + 1 = 5. If you write what you mean, you would have case Card is when 1 .. 13 => Put (Item => "Spades"); when 14 .. 26 => Put (Item => "Hearts"); when 27 .. 39 => Put (Item => "Diamonds"); when 40 .. 52 => Put (Item => "Clubs"); end case; which makes it easy to what the result is for any card, and since the subtype of the case expression is static, should compile. > type Sets_of_Card is array (Nombre_cartes range <>) of Cards; > type CARDS_SETS (Nb: Nombre_cartes := 0) is record > Set: Sets_of_Card (1..Nb) := INITIALIZATION (Nb); > end record; > pack.nb = 5 > pack.set'last = 5 > faulty line: > PACK := (pack.nb-1, Pack.Set(pack.set'First..Pack.Nb-1)); > issue: > raised CONSTRAINT_ERROR : discriminant check failed > > and why is this expression not rejected: > Pack := (pack.nb+1, pack.set & Card); > while this one is rejected ? > Pack := (pack.nb+1, pack.set & CARDS_SET'(1=> Card)); > a95cop1.adb:126:39: invalid operand types for operator "&" Assuming Pack is of type Cards_Sets, then Pack.Set has type Sets_Of_Card and the right operand of "&" is explicitly stated to be Cards_Set (which we have not seen), and apparently you have not defined "&" for these two types. > this works but overkill... -> Pack := (pack.nb+1, (pack.set with delta PACK.NB+1 => Card)); What are the declarations of Nombre_Cartes and Pack? -- Jeff Carter "Blessed is just about anyone with a vested interest in the status quo." Monty Python's Life of Brian 73