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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,227757d168eaa8a5 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!news.glorb.com!newshub.sdsu.edu!elnk-nf2-pas!elnk-pas-nf1!newsfeed.earthlink.net!cyclone.socal.rr.com!cyclone2.kc.rr.com!news2.kc.rr.com!twister.socal.rr.com.POSTED!53ab2750!not-for-mail Newsgroups: comp.lang.ada Subject: Re: A question re meaning/use of the "for ... use ..." References: <41b3291e$0$44072$5fc3050@dreader2.news.tiscali.nl> From: Keith Thompson Message-ID: User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux) Cancel-Lock: sha1:oKjMYczolSSNvbnHfLvGzoSTDtc= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Fri, 10 Dec 2004 09:23:12 GMT NNTP-Posting-Host: 66.91.240.168 X-Complaints-To: abuse@rr.com X-Trace: twister.socal.rr.com 1102670592 66.91.240.168 (Fri, 10 Dec 2004 01:23:12 PST) NNTP-Posting-Date: Fri, 10 Dec 2004 01:23:12 PST Organization: RoadRunner - West Xref: g2news1.google.com comp.lang.ada:6878 Date: 2004-12-10T09:23:12+00:00 List-Id: "David C. Hoos" writes: > Is there some reason the following code is not portable" > > package Enums > is > type Sparse_Enum is (One, Ten, One_Hundred, One_Thousand); > for Sparse_Enum use > (One => 1, Ten => 10, One_Hundred => 100, One_Thousand => 1000); > end Enums; > > with Enums; > with Ada.Exceptions; > with Ada.Text_IO; > with Ada.Unchecked_Conversion; > procedure Test_Enums > is > type Sparse_enum_Rep is range 0 .. 2**Enums.Sparse_Enum'Size - 1; > > function To_Rep is new Ada.Unchecked_Conversion > (Source => Enums.Sparse_Enum, Target => Sparse_enum_Rep); > > function To_Enum is new Ada.Unchecked_Conversion > (Target => Enums.Sparse_Enum, Source => Sparse_enum_Rep); > > function From_Rep (Rep : Sparse_enum_Rep) return Enums.Sparse_Enum > is > Enum : Enums.Sparse_Enum := To_Enum (Rep); > begin > if Enum'Valid then > return Enum; > end if; > Ada.Exceptions.Raise_Exception > (E => Constraint_Error'Identity, > Message => Sparse_enum_Rep'Image (Rep) & > " is an invalid representation of type Enums.Sparse_Enum."); > end From_Rep; > > begin > Ada.Text_IO.Put_Line ("Sparse_Enum representations:"); > Ada.Text_IO.New_Line; > > for N in Enums.Sparse_Enum loop > Ada.Text_IO.Put_Line > (Enums.Sparse_Enum'Image (N) & " => " & > Sparse_enum_Rep'Image (To_Rep (N))); > end loop; > > Ada.Text_IO.New_Line (2); > > Ada.Text_IO.Put_Line > ("Attempts to convert possible representations to Sparse_Enum."); > Ada.Text_IO.New_Line; > > for I in Sparse_enum_Rep'First .. Sparse_enum_Rep'First + 12 loop > begin > Ada.Text_IO.Put_Line > (Sparse_enum_Rep'Image (I) & " => " & > Enums.Sparse_Enum'Image (From_Rep (I))); > Ada.Text_IO.New_Line; > exception > when E: others => > Ada.Text_IO.Put_Line (Ada.Exceptions.Exception_Information (E)); > end; > end loop; > > end Test_Enums; Yes. Try changing the declaration of Sparse_Enum to: type Sparse_Enum is (Minus_One, One, Ten, One_Hundred, One_Thousand); for Sparse_Enum use (Minus_One => -1, One => 1, Ten => 10, One_Hundred => 100, One_Thousand => 1000); It shows the representation of Minus_One as 2047 rather than -1, and the loop runs from 0 to 12, skipping Minus_One. -- Keith Thompson (The_Other_Keith) kst-u@mib.org San Diego Supercomputer Center <*> We must do something. This is something. Therefore, we must do this.