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: a07f3367d7,7ff1de84a8945e80 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news1.google.com!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post02.iad.highwinds-media.com!news.flashnewsgroups.com-b7.4zTQh5tI3A!not-for-mail Newsgroups: comp.lang.ada Subject: Re: Access types as parameters References: <521c4843-d40f-4545-9e80-ca725e847090@h21g2000yqa.googlegroups.com> <8410fc60-9b8a-4f82-92fc-622a6bbe5931@i18g2000pro.googlegroups.com> <8880c3d0-a07f-4d4e-ac87-372014598576@d15g2000prc.googlegroups.com> From: Stephen Leake Date: Wed, 22 Jul 2009 22:11:29 -0400 Message-ID: User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.2 (windows-nt) Cancel-Lock: sha1:bDejgFVWw82YnywefpXEUjNI21o= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: abuse@flashnewsgroups.com Organization: FlashNewsgroups.com X-Trace: 3da9d4a67c6c5e197caa726685 Xref: g2news2.google.com comp.lang.ada:7286 Date: 2009-07-22T22:11:29-04:00 List-Id: Adam Beneschan writes: > For a procedure, there's less reason to do so. I don't know anything > about GTK, so I don't know why this wouldn't have worked, if all > Initialize is doing is to set up some fields in Button: > > procedure Initialize > (Button : in out Gtk_Button_Record'Class; > Label : UTF8_String); > > If, on the other hand, Initialize needs Button as an access so that it > can store it in a data structure, then it probably would have been > best to make it a named access type, to ensure that dangling > references aren't stored in that data structure. Most GTK subprograms do need access values; the same is true of any system that deals with derived types at multiple levels. The only library-level object that can hold any type in the hierarchy is a class-wide pointer. Lists of objects must hold pointers, etc. So one reason to use 'access' instead of 'in out' is simply to avoid the user having to type '.all' everywhere. A reason to use 'access Record_Type' instead of 'in Access_Type' is to avoid explicit type conversions. Given: package Widget is type Gtk_Widget_Record is ...; type Gtk_Widget is access all Gtk_Widget_Record'class; procedure Show (Widget : in Gtk_Widget); end Widget; package Window is type Gtk_Window_Record is new Gtk_Widget_Record with ...; type Gtk_Window is access all Gtk_Window_Record'class; end Window; Window : Gtk_Window := ...; then this does not work: Widget.Show (Window); but this does: Widget.Show (Gtk_Widget (Window)); This is just annoying! However, if Show is declared: procedure Show (Widget : access constant Gtk_Widget_Record'class); Then Show matches any access type in the class hierarchy; Widget.Show (Window); works. In addition, leaving out the 'class makes Show a primitive operation, which has many advantages. I've been struggling with this issue in OpenToken, and settled on using 'access Record_Type' as the best compromise. Now I just need to add 'constant' in all the right places; that wasn't allowed in Ada 95, when OpenToken was first written. In some cases, I have both class-wide and primitive operations: type Instance is abstract tagged private; subtype Class is Instance'Class; type Handle is access all Class; function Name (Token : in Instance) return String is abstract; -- Dispatching calls to Name function Name_Dispatch (Token : in Class) return String; function Name_Dispatch (Token : access constant Instance'Class) return String; That gives the best of all cases, at the expense of writting more code. > And if you're going to store the access parameter in a global > structure, you'll get a runtime error if you try to store a dangling > reference, so it's best to use a global access type so that > accessibility level errors are caught at compile time. This is a problem; people keep stumbling across it. Still, you get used to it after a while, and stop trying to pass 'access of a local variable. -- -- Stephe