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=0.0 required=3.0 tests=BAYES_40,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.5-pre1 X-Received: by 2002:a05:6214:1d05:: with SMTP id e5mr205711qvd.36.1615584152505; Fri, 12 Mar 2021 13:22:32 -0800 (PST) X-Received: by 2002:a25:ac52:: with SMTP id r18mr21792104ybd.303.1615584152268; Fri, 12 Mar 2021 13:22:32 -0800 (PST) Path: eternal-september.org!reader02.eternal-september.org!weretis.net!feeder8.news.weretis.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: Fri, 12 Mar 2021 13:22:32 -0800 (PST) In-Reply-To: <89128f73-fcc5-4e57-8067-d09877ba0211n@googlegroups.com> Injection-Info: google-groups.googlegroups.com; posting-host=2a01:799:620:ab00:d8d1:fa78:e1ad:6dc2; posting-account=uulyKwoAAAA86DO0ODu--rZtbje8Sytn NNTP-Posting-Host: 2a01:799:620:ab00:d8d1:fa78:e1ad:6dc2 References: <89128f73-fcc5-4e57-8067-d09877ba0211n@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: array from static predicate on enumerated type From: Egil H H Injection-Date: Fri, 12 Mar 2021 21:22:32 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Xref: reader02.eternal-september.org comp.lang.ada:61504 List-Id: On Friday, March 12, 2021 at 9:49:29 PM UTC+1, Matt Borchers wrote: > Say, for example, I define a static predicate on a sub-type of an enumera= ted type, like:=20 >=20 > type LETTERS is ( A, B, C, D, E, F, G, H, I , J, K );=20 >=20 > subtype CURVED is LETTERS=20 > with Static_Predicate CURVED in B | C | D | G | J;=20 >=20 > What I want is an array over CURVED (using CURVED as the index), but sinc= e attributes 'First and 'Last (and thus 'Range) is not allowed, this cannot= be done.=20 >=20 You should read what the Rationale says about predicates (http://www.ada-au= th.org/standards/12rat/html/Rat12-2-5.html), especially the part about 'First_Valid and 'Last_Valid: "These rules can also be illustrated by considering the dartboard. We might= like to accumulate a count of the number of times each particular score ha= s been achieved. So we might like to declare type Hit_Count is array (Score) of Integer; -- illegal but sadly this would result in an array with holes and so is forbidden. How= ever, we could declare an array from 1 to 60 and then initialize it with 0 = for those components used for hits and =E2=80=931 for the unused components= . Of course, we ought not to repeat literals such as 1 and 60 because of po= tential maintenance problems. But, we can use new attributes First_Valid an= d Last_Valid thus type Hit_Count is array (Score'First_Valid .. Score'Last_Valid) of Integer = :=3D (Score =3D> 0, others =3D> =E2=80=931); which uses Score to indicate the used components. The attributes First_Vali= d and Last_Valid can be applied to any static subtype but are particularly = useful with static predicates. In detail, First_Valid returns the smallest valid value of the subtype. It = takes any range and/or predicate into account whereas First only takes the = range into account. Similarly Last_Valid returns the largest value. Inciden= tally, they are illegal on null subtypes (because null subtypes have no val= id values at all)."