Roberto Leinardi
02/27/2023, 10:02 AMg_string_append_printf
, which is mapped with this:
@kotlinx.cinterop.internal.CCall public external fun g_string_append_printf(string: kotlinx.cinterop.CValuesRef<native.glib.GString /* = native.glib._GString */>?, @kotlinx.cinterop.internal.CCall.CString format: kotlin.String?, vararg variadicArguments: kotlin.Any?): kotlin.Unit { /* compiled code */ }
It seems to expect a vararg variadicArguments: kotlin.Any?
, but if I send that to it like this:
public fun appendPrintf(format: String, vararg variadicArguments: Any): Unit {
g_string_append_printf(cPointer.reinterpret(), format, variadicArguments)
}
I get this error when I build the module:
type kotlin.Array<out kotlin.Any> is not supported here: doesn't correspond to any C type
The GIR parameter only gives a name (...
) and has a tag `varargs`:
<parameter name="..." transfer-ownership="none">
<doc xml:space="preserve"
filename="glib-2.0.c"
line="32361">the parameters to insert into the format string</doc>
<varargs/>
</parameter>
But there are no other information about the type.
I am an Android developer and not very familiar with C Interop. Does anyone have a suggestion of what this vararg variadicArguments: kotlin.Any?
should be converted to?Adam S
02/27/2023, 10:54 AMRoberto Leinardi
02/27/2023, 11:06 AMAdam S
02/27/2023, 11:07 AMvararg Any
- but there’s a workaround (maybe - I’m not 100% sure)Roberto Leinardi
02/27/2023, 11:12 AMCValues<ShortVar>
, CValues<ByteVar>()
, etc type and forward that to the native call and throw an IllegalArgumentException
if is not one of the supported type?
I don't know if this make sense, I just saw that the compiler doesn't throws error if I send cValuesOf(1, 2, 3)
instead of the vararg
Adam S
02/27/2023, 11:15 AMRoberto Leinardi
02/27/2023, 11:20 AMvalues: CValues<out CPrimitiveVar>
Could this be a good option?public fun appendPrintf(format: String, vararg variadicArguments: CValues<out CPrimitiveVar>): Unit {
when (variadicArguments.size) {
0 -> g_string_append_printf(cPointer.reinterpret(), format)
1 -> g_string_append_printf(cPointer.reinterpret(), format, variadicArguments[0])
2 -> g_string_append_printf(cPointer.reinterpret(), format, variadicArguments[0], variadicArguments[1])
3 -> g_string_append_printf(cPointer.reinterpret(), format, variadicArguments[0], variadicArguments[1], variadicArguments[2])
else -> error("appendPrintf() can only accept up to 3 variable number of arguments (vararg)")
}
}
Big Chungus
02/27/2023, 11:58 AMRoberto Leinardi
02/27/2023, 12:09 PMClocks
02/27/2023, 5:20 PMRoberto Leinardi
02/27/2023, 6:26 PMCPointer<out CPrimitiveVar>
will cover all the cases. I can't use Any
, @Clocks do you have any suggestion for a better type?Big Chungus
02/27/2023, 6:48 PMRoberto Leinardi
02/27/2023, 6:57 PMBig Chungus
02/27/2023, 6:57 PMephemient
02/27/2023, 7:08 PMva_list
or C varargs, if you really need dynamic varargs. there's no platform-independent way to do it.Roberto Leinardi
02/27/2023, 7:16 PMva
too much in there)ephemient
02/27/2023, 7:19 PM