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!news-feed01.roc.ny.frontiernet.net!nntp.frontiernet.net!nntp.giganews.com.MISMATCH!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!cyclone1.gnilink.net!gnilink.net!news-east.rr.com!news.rr.com!news-server.columbus.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:/9tsULGl+9SuEvGikIjHOdS4M/U= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Wed, 08 Dec 2004 23:00:08 GMT NNTP-Posting-Host: 66.91.240.168 X-Complaints-To: abuse@rr.com X-Trace: twister.socal.rr.com 1102546808 66.91.240.168 (Wed, 08 Dec 2004 15:00:08 PST) NNTP-Posting-Date: Wed, 08 Dec 2004 15:00:08 PST Organization: RoadRunner - West Xref: g2news1.google.com comp.lang.ada:6853 Date: 2004-12-08T23:00:08+00:00 List-Id: "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.