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 Path: g2news2.google.com!postnews.google.com!d15g2000prc.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Access types as parameters Date: Tue, 21 Jul 2009 07:34:53 -0700 (PDT) Organization: http://groups.google.com Message-ID: <8880c3d0-a07f-4d4e-ac87-372014598576@d15g2000prc.googlegroups.com> References: <521c4843-d40f-4545-9e80-ca725e847090@h21g2000yqa.googlegroups.com> <8410fc60-9b8a-4f82-92fc-622a6bbe5931@i18g2000pro.googlegroups.com> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1248186894 31967 127.0.0.1 (21 Jul 2009 14:34:54 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 21 Jul 2009 14:34:54 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: d15g2000prc.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.5.30729; .NET CLR 3.0.30618),gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:7240 Date: 2009-07-21T07:34:53-07:00 List-Id: On Jul 19, 3:57=A0pm, rickduley wrote: > Hi Randy > > You wrote: > > So it is best to avoid the first form unless you have a particular need= for > > dispatching on an access value (which won't happen here, because the > > designated type is class-wide). > > Why then does GtkAda consistently use the first form, i.e.: > function My_Function (Thing : access My_Type'Class) return Positive; > for an 'Initialize' function? > > It actually uses the form (this for Gtk.Button.Gtk_Button): > procedure Initialize > =A0 (Button : access Gtk_Button_Record'Class; > =A0 =A0Label =A0: UTF8_String); Well, for a *function*, using an access parameter can help get around the rule that you can't have an IN OUT parameter to a function (a rule that I think is going away in the next revision of the language). By making it an anonymous access type, rather than a named access type, that lets you pass local variables that the function can modify. 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. If Initialize is an imported routine from a C library---well, in that case, I guess you do whatever works. But I'd still think that using "in out" would have the same effect---the address of the record is passed. (In fact, for an anonymous access parameter, the code normally CANNOT pass *just* an address; there has to be additional information in case the procedure needs to do an accessibility level check. But for a procedure imported from C, the parameter passing mechanisms may be different.) Randy is right, and I thought about saying something along those lines in my first response but decided (maybe unwisely) not to complicate things. You don't need to pass an anonymous access parameter if all you're going to do is modify the accessed object---IN OUT will do. 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. There may be some esoteric situations, other than dispatching, where passing an anonymous access parameter may be useful: perhaps the procedure can determine at runtime whether to store an access value or make a copy of the object depending on the accessibility level, or perhaps the procedure wants the ability to accept NULL to indicate that there is no object to modify, or perhaps a procedure takes two access parameters and sets up the two accessed objects to point to each other. But those all seem very unusual, and may be due to an inferior design anyway. -- Adam