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=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,e6acc909c7f4dc05,start X-Google-Attributes: gid103376,public From: woodruff@tanana.llnl.gov (John Woodruff) Subject: Type conversions forced by derived type: alternative? Date: 1996/09/04 Message-ID: #1/1 X-Deja-AN: 178501557 organization: Lawrence Livermore National Lab reply-to: woodruff1@llnl.gov newsgroups: comp.lang.ada Date: 1996-09-04T00:00:00+00:00 List-Id: In a recent discussion about "Two ideas for the next Ada Standard" Robert offered the advice > If you are writing a lot of type conversions (I guess that is what you > mean by using the Algol-68 term cast), then probably you are making > a common beginner's mistake of using derived types *too* heavily. > Yes, it is a strong point of Ada that you can use derived types > to separate different kinds of types, but is a weak point of many > Ada programs that they overuse this feature and use derived types > where subtypes would be more appropriate. Since I have been bothered recently about making a lot of such conversions, let me ask whether I am overlooking a simple way to avoid the common (not a beginner, I ;-) mistake. The type I'm declaring is private and is completed by providing an ADT instantiation within the private part: with Tree; -- from Booch components package Junk is type Object is private; procedure Do_Something (O : Object) ; private type Node is record Label : String (1..5) ; -- for this exercise end record; package Implementation is new Tree (Item => Node, Expected_Number_Of_Children => 5) ; type Object is new Implementation.Tree; -- subtype doesn't fit here end ; The consequence is that inside the body of this package (and its children) everyplace I want to operate on the structure of the Object, I have to convert the parameter of type Object into an Implementation.Tree -- ad infinitum --. package body Junk is procedure Do_Something (O : Object) is use Implementation ; begin -- See what I mean? if Item_Of(Implementation.Tree (O)).Label = "Hello" then null; end if ; end Do_Something; end ; I am pretty tired of that! If there is an alternative, please show me. It's ugly and I'm not very good at writing the conversions, so I make a certain number of errors. (Is there a way that renaming, or a Use declaration, or whatever, can let me say Item_Of(O) instead of the conversion I have here? -- John Woodruff N I F \ ^ / Lawrence Livermore National Lab =====---- < 0 > 510 422 4661 / v \