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=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI autolearn=unavailable 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!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!proxad.net!usenet-fr.net!enst.fr!melchior!cuivre.fr.eu.org!melchior.frmug.org!not-for-mail From: "David C. Hoos" Newsgroups: comp.lang.ada Subject: Re: A question re meaning/use of the "for ... use ..." Date: Thu, 9 Dec 2004 21:13:38 -0600 Organization: Cuivre, Argent, Or Message-ID: References: <41b3291e$0$44072$5fc3050@dreader2.news.tiscali.nl> NNTP-Posting-Host: lovelace.ada-france.org Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Trace: melchior.cuivre.fr.eu.org 1102648464 66738 212.85.156.195 (10 Dec 2004 03:14:24 GMT) X-Complaints-To: usenet@melchior.cuivre.fr.eu.org NNTP-Posting-Date: Fri, 10 Dec 2004 03:14:24 +0000 (UTC) Cc: "comp.lang.ada@ada.eu.org" To: "Keith Thompson" Return-Path: X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1437 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at ada-france.org X-BeenThere: comp.lang.ada@ada-france.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Gateway to the comp.lang.ada Usenet newsgroup" List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Xref: g2news1.google.com comp.lang.ada:6876 Date: 2004-12-09T21:13:38-06:00 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; Program output: Sparse_Enum representations: ONE => 1 TEN => 10 ONE_HUNDRED => 100 ONE_THOUSAND => 1000 Attempts to convert possible representations to Sparse_Enum. Exception name: CONSTRAINT_ERROR Message: 0 is an invalid representation of type Enums.Sparse_Enum. Call stack traceback locations: 0x8049ecc 0x804a0c3 0x8049d7a 0x42015502 1 => ONE Exception name: CONSTRAINT_ERROR Message: 2 is an invalid representation of type Enums.Sparse_Enum. Call stack traceback locations: 0x8049ecc 0x804a0c3 0x8049d7a 0x42015502 Exception name: CONSTRAINT_ERROR Message: 3 is an invalid representation of type Enums.Sparse_Enum. Call stack traceback locations: 0x8049ecc 0x804a0c3 0x8049d7a 0x42015502 Exception name: CONSTRAINT_ERROR Message: 4 is an invalid representation of type Enums.Sparse_Enum. Call stack traceback locations: 0x8049ecc 0x804a0c3 0x8049d7a 0x42015502 Exception name: CONSTRAINT_ERROR Message: 5 is an invalid representation of type Enums.Sparse_Enum. Call stack traceback locations: 0x8049ecc 0x804a0c3 0x8049d7a 0x42015502 Exception name: CONSTRAINT_ERROR Message: 6 is an invalid representation of type Enums.Sparse_Enum. Call stack traceback locations: 0x8049ecc 0x804a0c3 0x8049d7a 0x42015502 Exception name: CONSTRAINT_ERROR Message: 7 is an invalid representation of type Enums.Sparse_Enum. Call stack traceback locations: 0x8049ecc 0x804a0c3 0x8049d7a 0x42015502 Exception name: CONSTRAINT_ERROR Message: 8 is an invalid representation of type Enums.Sparse_Enum. Call stack traceback locations: 0x8049ecc 0x804a0c3 0x8049d7a 0x42015502 Exception name: CONSTRAINT_ERROR Message: 9 is an invalid representation of type Enums.Sparse_Enum. Call stack traceback locations: 0x8049ecc 0x804a0c3 0x8049d7a 0x42015502 10 => TEN Exception name: CONSTRAINT_ERROR Message: 11 is an invalid representation of type Enums.Sparse_Enum. Call stack traceback locations: 0x8049ecc 0x804a0c3 0x8049d7a 0x42015502 Exception name: CONSTRAINT_ERROR Message: 12 is an invalid representation of type Enums.Sparse_Enum. Call stack traceback locations: 0x8049ecc 0x804a0c3 0x8049d7a 0x42015502 Process run finished ----- Original Message ----- From: "Keith Thompson" Newsgroups: comp.lang.ada To: Sent: Wednesday, December 08, 2004 5:00 PM Subject: Re: A question re meaning/use of the "for ... use ..." > "Randy Brukardt" writes: > > "Keith Thompson" wrote in message > > news:ln8y8932ei.fsf@nuthaus.mib.org... > [...] > >> How do you portably choose the target type for the Unchecked_Conversion? > >> > >> The 'Pos attribute returns a result of type universal_integer; there's > >> no way to make an Unchecked_Conversion return a universal_integer. > > > > You have to declare a type for that purpose, but otherwise there is no > > problem: > > > > First_Rep : constant := ; > > Last_Rep : constant := ; > > type Enum is (First, ...., Last); > > for Enum use (First => First_Rep, .... Last => Last_Rep); > > type Enum_Rep is range First_Rep .. Last_Rep; > > for Enum_Rep'Size use Enum'Size; > > function To_Rep is new Ada.Unchecked_Conversion (Enum, Enum_Rep); > > function From_Rep is new Ada.Unchecked_Conversion (Enum_Rep, Enum); > > > > All of this is portable, and required for any Annex C compliant compiler. > > The only loss here is writing a bit of extra text (these declarations, and > > type conversions on the results of the functions). This is a rare enough > > need that that doesn't seem too bad. > > That's fine if the author of the code that declares the type Enum has > also bothered to declare the type Enum_Rep (and has done so > correctly). > > But suppose I'm using some third-party package that declares an > enumeration type. I have no control over the coding of the package. > The type may or may not have a representation clause; if it doesn't, > it may in the next version. As a client of the package, I don't have > enough information to declare the type Enum_Rep myself. > > It would also be nice to have an operation like From_Rep that raises > Constraint_Error if I call it with an invalid value. > > The language could have made this easier, and made enumeration > representation clauses more useful. In my opinion, it went to far in > trying to hide representation details from the user. > > -- > 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. > _______________________________________________ > comp.lang.ada mailing list > comp.lang.ada@ada-france.org > http://www.ada-france.org/mailman/listinfo/comp.lang.ada >