问题描述
Vala是否支持自调用?是使用类还是方法?
Is there any way that Vala supports Self Invoking? Either with a class, or with a method?
JavaScript支持如下所示的自调用.我在找什么.
Javascript supports self invoking like below. Which is what im looking for.
(function(){
// some code…
})();
我正在尝试将一个类加载到哈希图中以进行动态加载.
I'm attempting to load a class into a hashmap for dynamically loading.
推荐答案
using Gee;
[CCode (has_target = false)]
delegate void MyDelegate();
int main() {
var map = new HashMap<string, MyDelegate>();
map["one"] = () => { stdout.printf("1\n"); };
map["two"] = () => { stdout.printf("2\n"); };
MyDelegate d = map["two"];
d();
return 0;
}
如果在委托中需要目标,则必须编写包装器,请参见以下问题:包含方法作为值的Gee HashMap
If you need a target in your delegate you have to write a wrapper, see this question:Gee HashMap containing methods as values
如您所见,您不需要自我发票.自我报价看起来像这样:
As you can see, you don't need self invokation. Self invokation would look something like this:
int main() {
(() => { stdout.printf("Hello world!\n"); })();
return 0;
}
Vala不支持此功能(我使用valac-0.22对其进行了测试).
This is not supported by Vala (I tested this with valac-0.22).
调用委托var可以按预期工作:
Invoking a delegate var works as expected:
delegate void MyDelegate();
int main() {
MyDelegate d = () => { stdout.printf("Hello world!\n"); };
d();
return 0;
}
这篇关于Vala是否支持自调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!