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.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,26a21b9e317dc639 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII Received: by 10.224.105.205 with SMTP id u13mr9419977qao.6.1353356544170; Mon, 19 Nov 2012 12:22:24 -0800 (PST) Received: by 10.49.116.1 with SMTP id js1mr1023250qeb.19.1353356544143; Mon, 19 Nov 2012 12:22:24 -0800 (PST) Path: gf5ni327qab.0!nntp.google.com!u2no13581305qal.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Mon, 19 Nov 2012 12:22:21 -0800 (PST) In-Reply-To: <9b0bcb37-8ae3-440f-af4f-a796702e4250@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=85.227.237.85; posting-account=l8k8IwoAAADeDydswOzwNzmn10qOk9gt NNTP-Posting-Host: 85.227.237.85 References: <9b0bcb37-8ae3-440f-af4f-a796702e4250@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <8906e1e3-3b18-4b37-ac46-3857db47cea4@googlegroups.com> Subject: Re: Access type to member procedure of instance (Object Oriented programming in Ada) From: ake.ragnar.dahlgren@gmail.com Injection-Date: Mon, 19 Nov 2012 20:22:24 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Date: 2012-11-19T12:22:21-08:00 List-Id: On Monday, November 19, 2012 10:59:42 AM UTC+1, ake.ragna...@gmail.com wrot= e: > Not sure if this has already been discussed but: Is it possible to define= a pointer to a member procedure of an instance? >=20 >=20 >=20 > This problem has it's origin in the implementation of the MVC pattern for= a GtkAda application. Consider the following: >=20 >=20 >=20 > package Some_Package is >=20 >=20 >=20 > type Message_Type is tagged null record; >=20 > =20 >=20 > procedure Print(Message : Message_Type); >=20 >=20 >=20 > end Some_Package; >=20 >=20 >=20 >=20 >=20 >=20 >=20 > package body Some_Package is >=20 >=20 >=20 > procedure Print (Message : Message_Type) is >=20 > begin >=20 > null; >=20 > end Print; >=20 >=20 >=20 > end Some_Package; >=20 >=20 >=20 >=20 >=20 > Then it is possible to create the following Main application that compile= s just fine: >=20 >=20 >=20 > with Some_Package; >=20 >=20 >=20 > procedure Main is >=20 > Message : Some_Package.Message_Type; >=20 > begin >=20 > Message.Print; >=20 > end Main; >=20 >=20 >=20 > Is it possible to create a parameterless access type to the Message.Print= procedure? The following application refuses to compile: >=20 >=20 >=20 > with Some_Package; >=20 >=20 >=20 > procedure Main is >=20 > Message : Some_Package.Message_Type; >=20 > =20 >=20 > Method_Ref : access procedure :=3D Message.Print'Access; >=20 > begin >=20 > Method_Ref; >=20 > end Main; >=20 >=20 >=20 > The error message is: no selector "Print" for type "Message_Type" defined= at some_package.ads:3 >=20 >=20 >=20 >=20 >=20 > It is possible to create a workaround: >=20 >=20 >=20 > with Some_Package; >=20 >=20 >=20 > procedure Main is >=20 > Message : Some_Package.Message_Type; >=20 > =20 >=20 > procedure Method_Ref is >=20 > begin >=20 > Message.Print; >=20 > end Method_Ref; >=20 > =20 >=20 > begin >=20 > Method_Ref; >=20 > end Main; >=20 >=20 >=20 > Is there a better way? Or is this the optimal way? >=20 >=20 >=20 > Regards, >=20 > =C5ke Ragnar Dahlgren Thanks everybody for posting. Adam you answered my question perfectly since= my question was a pure Ada question whether or not it is possible to defin= e pointers to member methods of class instances. And the answer is no. To a= dd this as a feature to Ada might be useful. But of course this needs caref= ul investigation. I do not mean to make this a Gtkada issue but I have received requests for = clarifying the origin of the question. The Gtk application uses Glade and contains the following procedure call: Gtkada.Builder.Register_Handler (Builder =3D> Builder, Handler_Name =3D> "File_Quit", Handler =3D> On_Quit'Access); where the On_Quit procedure is declared as follows: procedure On_Quit (Object : access Gtkada.Builder.Gtkada_Builder_Record'Cla= ss) is begin -- Code is omitted end On_Quit; When On_Quit is defined as above everything compiles just fine. The problem= is if one would like the On_Quit method to be a member method of an instan= ce. In this case there is a Controller instance whose method should be call= ed. It is declared as: procedure On_Quit (This : Controller_Type; Object : access Gtkada.Builder.Gtkada_Builder_Record'Cla= ss); Using the Controller's On_Quit procedure one would like to write: Gtkada.Builder.Register_Handler (Builder =3D> Builder, Handler_Name =3D> "File_Quit", Handler =3D> Controller.On_Quit'Access); This did not compile and thereof the question. Additional information if needed: The spec. of the Register_Handler procedu= re in the Gtkada.Builder package is declared as: procedure Register_Handler (Builder : access Gtkada_Builder_Record'Class; Handler_Name : String; Handler : Builder_Handler); The Builder_Handler is a pointer to a method: type Builder_Handler is access procedure (Builder : access Gtkada_Builder_Record'Class); Best regards, =C5ke Ragnar Dahlgren