VarArgs<T extends Record> class abstract final

The types of variadic arguments passed in C.

The signatures in NativeFunction need to specify the exact types of each actual argument used in FFI calls.

For example take calling printf in C.

int printf(const char *format, ...);

void call_printf() {
  int a = 4;
  double b = 5.5;
  const char* format = "...";
  printf(format, a, b);
}

To call printf directly from Dart with those two argument types, define the native type as follows:

/// `int printf(const char *format, ...)` with `int` and `double` as
/// varargs.
typedef NativePrintfIntDouble =
    Int Function(Pointer<Char>, VarArgs<(Int, Double)>);

Note the record type inside the VarArgs type argument.

If only a single variadic argument is passed, the record type must contain a trailing comma:

/// `int printf(const char *format, ...)` with only `int` as varargs.
typedef NativePrintfInt = Int Function(Pointer<Char>, VarArgs<(Int,)>);

When a variadic function is called with different variadic argument types, multiple bindings need to be created. To avoid doing multiple DynamicLibrary.lookups for the same symbol, the pointer to the symbol can be cast:

final dylib = DynamicLibrary.executable();
final printfPointer = dylib.lookup('printf');
final void Function(Pointer<Char>, int, double) printfIntDouble =
    printfPointer.cast<NativeFunction<NativePrintfIntDouble>>().asFunction();
final void Function(Pointer<Char>, int) printfInt =
    printfPointer.cast<NativeFunction<NativePrintfInt>>().asFunction();

If no variadic argument is passed, the VarArgs must be passed with an empty record type:

/// `int printf(const char *format, ...)` with no varargs.
typedef NativePrintfNoVarArgs = Int Function(Pointer<Char>, VarArgs<()>);

VarArgs must be the last parameter.

VarArgs is not constructible in the Dart code and serves purely as marker in type signatures.

Inheritance
Annotations
  • @Since('3.0')

Constructors

VarArgs()

Properties

hashCode int
The hash code for this object.
read-onlyinherited
runtimeType Type
A representation of the runtime type of the object.
read-onlyinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited