Page 1 of 1

Creating non-generic delegate when there is generic one

PostPosted: June 8th, 2013, 6:46 pm
by weirdan
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.

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

PostPosted: June 18th, 2013, 10:37 am
by Jakub Misek
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.

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

PostPosted: June 18th, 2013, 12:20 pm
by weirdan
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.

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

PostPosted: June 22nd, 2013, 9:57 pm
by weirdan
Implicit conversion worked, thank you for the pointer.

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

PostPosted: June 23rd, 2013, 10:33 am
by Jakub Misek
Great! I'm glad it works.

Thanks,