comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: "unconstrained subtype in component declaration" vs "discriminant in constraint must appear alone"
Date: Wed, 24 Feb 2021 11:04:58 +0100	[thread overview]
Message-ID: <s158ca$qt3$1@dont-email.me> (raw)
In-Reply-To: <6716a4e5-02ac-4735-83c9-28bff3f8aae6n@googlegroups.com>

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

  parent reply	other threads:[~2021-02-24 10:04 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-22  0:14 "unconstrained subtype in component declaration" vs "discriminant in constraint must appear alone" Mehdi Saada
2021-02-22  6:32 ` J-P. Rosen
2021-02-22 10:14   ` AdaMagica
2021-02-22 10:57     ` Dmitry A. Kazakov
2021-02-22 14:20       ` Mehdi Saada
2021-02-22 15:11         ` J-P. Rosen
2021-02-22 15:55         ` Dmitry A. Kazakov
2021-02-22 16:21           ` Mehdi Saada
2021-02-23 22:06             ` Mehdi Saada
2021-02-24  9:59               ` AdaMagica
2021-02-24 10:04               ` Jeffrey R. Carter [this message]
2021-02-24 14:11               ` Simon Wright
2021-02-24 15:57               ` Shark8
2021-02-25  0:13                 ` Mehdi Saada
2021-02-25  8:58                   ` AdaMagica
2021-02-25 10:25                     ` Mehdi Saada
2021-02-25 10:28                     ` Mehdi Saada
2021-02-22 10:35 ` Jeffrey R. Carter
2021-02-22 14:21   ` Mehdi Saada
2021-02-22 15:02     ` Jeffrey R. Carter
2021-02-22 15:08       ` J-P. Rosen
replies disabled

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