简体   繁体   English

如何在Vala中创建异步可变参数函数

[英]How to create an async variadic function in Vala

Is it possible to create an async variadic function in Vala? 是否可以在Vala中创建异步可变参数函数? If yes, how? 如果是,怎么办? I couldn't find anything related in the Vala tutorial provided on the gnome website or in any example of code. 在gnome网站上提供的Vala教程或任何代码示例中,我都找不到任何相关的内容。 My conclusion is that it's not possible, because vala requires async functions to have fixed arguments. 我的结论是,这是不可能的,因为vala需要异步函数具有固定的参数。 But then, i don't know how to achieve something similar to a variadic function. 但是然后,我不知道如何实现类似于可变参数的功能。

Example of code (non async, working without issues): 代码示例(非异步,正常运行):

void long_function(string first_val, ...) {
   var list = va_list();
   string? second_val = list.arg();
   print("%s,%s\n", first_val, second_val);
}
void main() {
  long_function("a", "b");
}

Example of async code (not working): 异步代码示例(无效):

async void long_function(string first_val, ...) {
    var list = va_list();
    string? second_val = list.arg();
    print("%s,%s\n", first_val, second_val);
}
void main() {
    long_function.begin("a", "b");
}

The error returned by the vala compiler (compiled with: vala --pkg gio-2.0 main.vala ) is vala编译器(编译为: vala --pkg gio-2.0 main.vala )返回的错误为

main.vala:7.28-7.30: error: Argument 2: Cannot convert from `unowned string' to `void GLib.AsyncReadyCallback? (GLib.Object?, GLib.AsyncResult)'

My real use case scenario is (pseudo code): 我的实际用例场景是(伪代码):

async void fetch_from_api_with_params(...) {
  // ExternalLibrary is a function which accepts a string with a url and any number of POST parameters
  ExternalLibrary.fetch_from_url.begin("http://example.com", va_list());
  // ...
}

Sadly, this is not possible with Vala. 遗憾的是,Vala无法做到这一点。 Vala uses C's variadic arguments system and GLib's co-routine system. Vala使用C的可变参数系统和GLib的协同例程系统。 Unfortunately, the two aren't compatible. 不幸的是,两者不兼容。 Depending on your needs, you might be able to pass an array of Variant . 根据您的需要,您可能可以传递Variant数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM