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,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.5-pre1 X-Received: by 2002:a37:78d:: with SMTP id 135mr32559243qkh.472.1614182271756; Wed, 24 Feb 2021 07:57:51 -0800 (PST) X-Received: by 2002:a5b:9d2:: with SMTP id y18mr47831669ybq.173.1614182271494; Wed, 24 Feb 2021 07:57:51 -0800 (PST) Path: eternal-september.org!reader02.eternal-september.org!news.uzoreto.com!news.muarf.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Wed, 24 Feb 2021 07:57:51 -0800 (PST) In-Reply-To: <6716a4e5-02ac-4735-83c9-28bff3f8aae6n@googlegroups.com> Injection-Info: google-groups.googlegroups.com; posting-host=146.5.2.231; posting-account=lJ3JNwoAAAAQfH3VV9vttJLkThaxtTfC NNTP-Posting-Host: 146.5.2.231 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> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: "unconstrained subtype in component declaration" vs "discriminant in constraint must appear alone" From: Shark8 Injection-Date: Wed, 24 Feb 2021 15:57:51 +0000 Content-Type: text/plain; charset="UTF-8" Xref: reader02.eternal-september.org comp.lang.ada:61436 List-Id: On Tuesday, February 23, 2021 at 3:06:19 PM UTC-7, 0012... wrote: > Please help figure out this: > > 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 > 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; Hm, I would recommend using an enumeration here. Type Suit is (Spades, Hearts, Clubs, Diamonds); -- Or whatever you have/terms you want. Function Get_Suit( Card : Cards ) return Suit is ( Suit'Val( Natural(Card)/13 ) ); Then use: Case Get_Suit(card) is When Diamonds => ... When Hearts => ... When Spades => ... When Clubs => ... 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) > > also what's wrong here: > Context => > 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 If you have something like PACK : CARD_SETS(7):= [...], then you cannot have a CARD_SETS(4) assigned to it. Likewise, if you have a PACK : CARD_SETS := (Nb => 3, [proper-set-of-cards]), you cannot assign a (Nb => 2, [proper-set-of-cards]) to it, because to do so would be to alter the discriminant. Now, one thing that you may not be cognizant of is that Sets_of_Cards is a full deck* of cards, so CARDS_SET(3) is three decks of cards. * -- But this isn't quite true, you could set the full range of the array to a single value, say 4. A bit of advice: Ada is really good at describing the problem-space using the type-system, so don't try to force the problem-space into some other domain. There is nothing wrong with saying Type Suit is (Hearts, Clubs, Diamonds, Spades); Type Color is (Red, Black); Function Is_Red(Item : Suit) return Boolean is ( Color'Val(Suit'Pos(Item) mod 2) ); Type Rank is ('1','2','3','4','5','6','7','8','9','0','J','Q','K','A'); -- Or numbers, card-ranking is kinda "squishy". Type Card is record My_Suit : Suit; My_Rank : Rank; end type; Function "<"(Left, Right: Card) return Boolean is --Ranking mechanism. --... > and why is this expression not rejected: > Pack := (pack.nb+1, pack.set & Card); > while this one is rejected ? I believe it's because you're trying to re-set the discriminant; without a suitable example it's hard to tell though. > Pack := (pack.nb+1, pack.set & CARDS_SET'(1=> Card)); > a95cop1.adb:126:39: invalid operand types for operator "&" > this works but overkill... -> Pack := (pack.nb+1, (pack.set with delta PACK.NB+1 => Card)); Yeah, here you're straight-up trying to overwrite the discriminant.