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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fac41,b87849933931bc93 X-Google-Attributes: gidfac41,public X-Google-Thread: 109fba,b87849933931bc93 X-Google-Attributes: gid109fba,public X-Google-Thread: 1108a1,b87849933931bc93 X-Google-Attributes: gid1108a1,public X-Google-Thread: f43e6,b87849933931bc93 X-Google-Attributes: gidf43e6,public X-Google-Thread: 114809,b87849933931bc93 X-Google-Attributes: gid114809,public X-Google-Thread: 103376,b87849933931bc93 X-Google-Attributes: gid103376,public From: eachus@spectre.mitre.org (Robert I. Eachus) Subject: Re: OO, C++, and something much better! Date: 1997/01/13 Message-ID: #1/1 X-Deja-AN: 209608959 references: organization: The Mitre Corp., Bedford, MA. newsgroups: comp.lang.c++,comp.lang.smalltalk,comp.lang.eiffel,comp.lang.ada,comp.object,comp.software-eng Date: 1997-01-13T00:00:00+00:00 List-Id: In article donh@syd.csa.com.au (Don Harrison) writes: > :> No. Ada offers type casting in a couple of ways: > :> > :> 1) UNCHECKED_CONVERSION and > :> 2) Address representation clauses (overlays). > : > :1. This is not "casting" > 1) is; 2) isn't. But both violate safe typing. Casting in C normally maps to type conversions in Ada: foo a; bar b; ... b = bar(a); Should be translated as: A: Foo; B: Bar; ... B := Bar(A); -- The case changes are due to standard Ada and C style. This in Ada is a completely type safe conversion. If you want to go outside the compilers type checking, you can use Unchecked_Conversion. But interpret what you are doing correctly. You the programmer are taking responsibility for doing type checking which the compiler is unable to do. There are many uses for Unchecked_Conversion where this is exactly what is going on... For example, in one version of the ADAR components, to preserve the visible (to the user of the components) type hierarchy, I had to do an Unchecked_Conversion in the body of one unit--from a derived type to its parent, but the actual derivation wasn't visible at that point in the code. Doing "punning" using Unchecked_Conversion from the formal point of view is an error, and Ada compilers are allowed (but not able in all cases) to detect such violations and raise Program_Error. For example, someone was just asking how to do a boolean or on two Ada strings. The answer posted was more complex than necessary because it used Unchecked_Conversion. An Ada character can be converted to a (universal) integer type, and "or" exists for unsigned integer types in Ada 95. So a better approach would be: function "or"(L,R: Character) return Character is type Byte is mod 256; begin return Character'Val(Byte(Character'Pos(L)) or Byte(Character'Pos(R))); end "or"; function "or"(L,R: String) return String is Result: String(1..L'Length) := L; begin if L'Length /= R'Length then raise Constraint_Error; end if; for I in Result'Range loop Result(I) := Result(I) or R(I-R'First+1); end loop; return Result; end "or"; With appropriate compiler inlining ;-) this should give results as good or better than the non-portable version, but that is another subject. -- Robert I. Eachus with Standard_Disclaimer; use Standard_Disclaimer; function Message (Text: in Clever_Ideas) return Better_Ideas is...