Is it possible to splat into ccall?

I’m aware of the importance of ensuring that functions are called with the correct signatures. There is nothing Julia can do to enforce this – when calling external functions, it’s the programmer’s responsibility to ensure that the function signature is defined correctly in Julia and that, for example, arguments correctly matching the format string are passed to functions like printf. And in C, this is just how it works – there is no way for printf to verify how many arguments were actually passed to it.

When it comes to making sure that the numbers of argument types and argument values are equal, there is no reason that it has to be done at a syntax level or at compile time – in the simplest case, ccall could just check if they match at runtime and throw an exception if they don’t. But this is really a flaw in the design of ccall: Why are the argument types and values passed separately? If they were passed together (which is exactly how it works with @ccall), there wouldn’t even be any need to check that the lengths match!

Rather, what I think is going on here is that this might be an accidental artifact of history rather than a conscious design decision. As @c42f explains in the thread I linked, ccall was created at a very early stage:

Perhaps splatting in its current form just didn’t exist at the time, and ccall simply incorrectly interprets a splatting expression as a single argument? The error message is definitely incorrect and misleading:

ERROR: syntax: more types than arguments for ccall around REPL[65]:1

Also, note that in my example, it’s guaranteed even at compile time that the correct number of arguments is passed, because args is of type NTuple{9, Integer}.

1 Like