Gee HashMap containing methods as values

I am trying to populate a Libgee HashMap, where each record has a line as a key and a function as a value. Is it possible? I want this kind of thing:

var keybindings = new Gee.HashMap<string, function> (); keybindings.set ("<control>h", this.show_help ()); keybindings.set ("<control>q", this.explode ()); 

so that I can eventually do something like this:

 foreach (var entry in keybindings.entries) { uint key_code; Gdk.ModifierType accelerator_mods; Gtk.accelerator_parse((string) entry.key, out key_code, out accelerator_mods); accel_group.connect(key_code, accelerator_mods, Gtk.AccelFlags.VISIBLE, entry.value); } 

But maybe this is not the best way?

+6
source share
2 answers

Delegates are what you are looking for. But the last time I checked, generics do not support delegates, so a not-so-elegant way is to wrap it:

 delegate void DelegateType(); private class DelegateWrapper { public DelegateType d; public DelegateWrapper(DelegateType d) { this.d = d; } } Gee.HashMap keybindings = new Gee.HashMap<string, DelegateWrapper> (); keybindings.set ("<control>h", new DelegateWrapper(this.show_help)); keybindings.set ("<control>q", new DelegateWrapper(this.explode)); //then connect like you normally would do: accel_group.connect(entry.value.d); 
+5
source

This is only possible for delegates with [CCode (has_target = false)], otherwise you need to create a wrapper as takoi suggested.

+2
source

Source: https://habr.com/ru/post/889156/