Creating non-generic delegate when there is generic one

Discussion about the open-source Phalanger [?] project.

Creating non-generic delegate when there is generic one

Postby weirdan » June 8th, 2013, 6:46 pm

Hello

I'm working on porting a php-gtk application to phalanger/gtk-sharp. Currently I'm stuck on the following problem:

There is an event (Gtk.MenuItem.Activated) that only accepts System.EventHandler delegates (but not System.EventHandler<TEventArgs> variety). When I try to create a delegate based on http://wiki.php-compiler.net/Code_Sampl ... chronously sample, it creates generic version:

Code: Select all
$h = new \System\EventHandler(function() {});
\var_dump($h);
$item = $this->ui->GetWidget('quit_menu_item');
\var_dump(\get_class($item), $item->Activated);
$item->Activated->Add($h);

Code: Select all
object(System\EventHandler<:System\Object:>)#1051914240 (2) {
  ["Method"]=>
  {Void <^DelegateStub>(PHP.Core.Reflection.DObject, System.Object, System.Object)}
  ["Target"]=>
  {object}
}
string(17) "Gtk\ImageMenuItem"
object(EventClass<:System\EventHandler:>)#380650752 (0) {
}

Error: Argument 'handler' must be of type 'System\EventHandler'.


So the question is: how do I create System\EventHandler instead of System\EventHandler<:T:> ?

PS: I also tried to inherit System\EventHandler in my own class, but unfortunately it's marked final and thus non-extendable.
weirdan
 
Posts: 17
Joined: June 4th, 2013, 1:11 am

Re: Creating non-generic delegate when there is generic one

Postby Jakub Misek » June 18th, 2013, 10:37 am

I guess Phalanger cannot have two classes with the same name "EventHandler", so it uses the last one it founds.

You can try implicit conversion,
Code: Select all
$item->Activated->Add(function(){});


Phalanger automatically converts callable PHP object to .NET delegate representation of correct type.

If it won't work, you can implement some factory method in C#, and call that helper method from PHP.
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague

Re: Creating non-generic delegate when there is generic one

Postby weirdan » June 18th, 2013, 12:20 pm

Jakub Misek wrote:If it won't work, you can implement some factory method in C#, and call that helper method from PHP.

I tried that, but I guess my sharp-fu is not strong enough. Will try implicit conversion once I get back from work.
weirdan
 
Posts: 17
Joined: June 4th, 2013, 1:11 am

Re: Creating non-generic delegate when there is generic one

Postby weirdan » June 22nd, 2013, 9:57 pm

Implicit conversion worked, thank you for the pointer.
weirdan
 
Posts: 17
Joined: June 4th, 2013, 1:11 am

Re: Creating non-generic delegate when there is generic one

Postby Jakub Misek » June 23rd, 2013, 10:33 am

Great! I'm glad it works.

Thanks,
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague


Return to Phalanger project

Who is online

Users browsing this forum: No registered users and 4 guests

cron

User Control Panel

Login

Who is online

In total there are 4 users online :: 0 registered, 0 hidden and 4 guests (based on users active over the past 5 minutes)
Most users ever online was 151 on December 6th, 2020, 7:46 am

Users browsing this forum: No registered users and 4 guests