comp.lang.ada
 help / color / mirror / Atom feed
From: Brian Drummond <brian@shapes.demon.co.uk>
Subject: Re: Access type to member procedure of instance (Object Oriented programming in Ada)
Date: Mon, 19 Nov 2012 11:39:03 +0000 (UTC)
Date: 2012-11-19T11:39:03+00:00	[thread overview]
Message-ID: <k8d5on$t0b$1@dont-email.me> (raw)
In-Reply-To: 9b0bcb37-8ae3-440f-af4f-a796702e4250@googlegroups.com

On Mon, 19 Nov 2012 01:59:42 -0800, ake.ragnar.dahlgren wrote:

> Not sure if this has already been discussed but: Is it possible to
> define a pointer to a member procedure of an instance?

Yes.

with Some_Package;

procedure Main is
   Message : Some_Package.Message_Type;

   Method_Ref : access procedure (Message : Some_Package.Message_Type) 
		:= Some_Package.Print'Access;
begin
   Method_Ref.all(Message);
end Main;

However this is not the same question as:

> Is it possible to create a parameterless access type to the
> Message.Print procedure? The following application refuses to compile:

Message.Print is not parameterless, it is identical to Print(Message) 
so ... no. 

(This is not specifically Ada, in any OO language I have ever seen, the 
receiver is always the first parameter. I don't know of any language that 
replicates every method for every instance of a class, but I suppose it's 
theoretically possible!. Unless "Message" is a singleton, it would be 
horribly inefficient)

You can certainly define a pointer to the member procedure, but it still 
expects a parameter, and you still have to supply that.

You can certainly wrap it in a parameterless procedure (binding Message 
to Print) and create a parameterless access to that... 
with Some_Package;

procedure Main is
   Message : Some_Package.Message_Type;

procedure PrintMessage is
begin
   Message.Print;
end PrintMessage;
   
   Method_Ref : access procedure := PrintMessage'Access;
begin
   Method_Ref.all;
end Main;

but I have a feeling you are trying to accomplish something else here and 
I can't see what. 

Hopefully someone else has a better idea...

- Brian



  parent reply	other threads:[~2012-11-22  3:27 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-19  9:59 Access type to member procedure of instance (Object Oriented programming in Ada) ake.ragnar.dahlgren
2012-11-19 11:13 ` Georg Bauhaus
2012-11-19 11:39 ` Brian Drummond [this message]
2012-11-20 11:43   ` Brian Drummond
2012-11-20 21:57     ` Randy Brukardt
2012-11-19 13:03 ` sbelmont700
2012-11-19 16:18 ` Adam Beneschan
2012-11-19 17:02   ` Peter C. Chapin
2012-11-19 18:23     ` Adam Beneschan
2012-11-19 20:57       ` Peter C. Chapin
2012-11-19 21:26       ` Dmitry A. Kazakov
2012-11-19 22:19         ` Adam Beneschan
2012-11-20 10:12           ` Dmitry A. Kazakov
2012-11-20 21:51             ` Randy Brukardt
2012-11-21  8:24               ` Dmitry A. Kazakov
2012-11-21 22:19                 ` Randy Brukardt
2012-11-20 10:59     ` Brian Drummond
2012-11-19 20:22 ` ake.ragnar.dahlgren
2012-11-20 11:16   ` Brian Drummond
2012-11-19 20:52 ` ake.ragnar.dahlgren
2012-11-19 21:56   ` Dmitry A. Kazakov
2012-11-22  9:49     ` ake.ragnar.dahlgren
2012-11-19 22:13   ` sbelmont700
2012-11-19 23:59 ` Randy Brukardt
2012-11-20  0:05   ` Randy Brukardt
2012-11-20  1:00     ` Adam Beneschan
2012-11-20 21:38       ` Randy Brukardt
2012-11-20 23:43         ` Adam Beneschan
2012-11-21 22:12           ` Randy Brukardt
2012-11-22  1:59             ` Adam Beneschan
2012-11-29  2:43               ` Randy Brukardt
2012-11-20  0:52   ` Adam Beneschan
2012-11-20 21:34     ` Randy Brukardt
2012-11-20 11:28   ` Brian Drummond
2012-11-20 14:27     ` Georg Bauhaus
2012-11-20 15:52     ` Adam Beneschan
2012-11-22  9:47 ` ake.ragnar.dahlgren
2012-11-22 10:25   ` Dmitry A. Kazakov
2012-12-02 20:42     ` ake.ragnar.dahlgren
2012-12-03 11:21       ` Dmitry A. Kazakov
2012-12-03 20:21         ` ake.ragnar.dahlgren
2012-12-03 22:15           ` Dmitry A. Kazakov
2012-12-25 21:51           ` Gustaf Thorslund
2012-12-26 18:11             ` ake.ragnar.dahlgren
2012-11-22 12:13   ` Brian Drummond
2012-12-03 16:17     ` ake.ragnar.dahlgren
2012-12-03 21:56       ` Brian Drummond
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox