简体   繁体   中英

How to create an async variadic function in Vala

Is it possible to create an async variadic function in 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. My conclusion is that it's not possible, because vala requires async functions to have fixed arguments. 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

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 uses C's variadic arguments system and GLib's co-routine system. Unfortunately, the two aren't compatible. Depending on your needs, you might be able to pass an array of Variant .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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