On calling variadic C functions

The fact that we can't call these kind of functions currently is a pain in the ass :sweat_smile:

Recently we were working on a Swift wrapper for some of these functions and even though now we're using va_lists to call the "non variadic alternative" to them, it was really quite a struggle to make our preliminary solutions work.

Is there a strong reason why we can't call variadic C functions, or is it just something in the to-do list that will land eventually?

-- Félix


Also: I think the compiler should at least point out that you can actually create va_lists from Swift using the functions at VarArg.swift:

  • error: variadic function call_bla(char *first, ...) not available. Try using call_blav(char *first, va_list args) instead. See how at <swift documentation on va_lists>

Formatted like a fixit would be even better :slight_smile:

1 Like

I always assumed variadic C functions are the main justification to keep variadics in Swift as we have them now - and now I'm told that it not even possible to simply use those functions? ;-)

It's a pity that none of the tries to unify variadics and Array arguments gained real traction :-(

It was a deliberate decision not to support arbitrary C variadic functions in Swift, since they are not type-safe and not composable, and don't have a good way to represent them in today's type system. (Never mind that you can't compose variadic Swift functions at the moment either, because we don't have forwarding…)

I agree that it would be nice to point the user towards withVaList if there is in fact a related function that takes a va_list/CVaList, but we'd have to be pretty confident in how the compiler finds such a "related" function before we suggest it. (The "v" suffix is pretty good for C functions; arguments: is frequently used for Objective-C methods.)

1 Like

Ohh, I see. Yeah, that makes sense (also, while we're at it: dangit, C is so unsafe! More than I already knew!).

For the compiler help, I think this is acceptable:

  • Happy path: compiler finds the non-variadic variant (send ~> sendv for C, <something something>, arguments: for ObjC). Then it recommends trying it instead and points out to the docs.
  • Normal path: compiler says there's maybe a non-variadic form of the function, and points out to the docs.
  • (maybe) At all paths: point out to somewhere in the docs where we say why variadic C functions aren't imported in Swift. That document explains what you've told me and gives some alternatives (like looking for functions that accept va_list).
1 Like

I've created an entry at JIRA regarding this :slight_smile: