invoke

Correctly calls a function with the given arguments.

If F is not a Callable (e.g lambda infered as void) then F is called with args. If F is Callable and is a member function of args[0], then the function is called using args[0] as the instance with args[1..$] as the parameters. Otherwise, F is called normally. The arguments are attempted to be forwarded with tryForward.

  1. auto ref invoke(Args args)
    package ref
    invoke
    (
    Args...
    )
    (
    auto ref Args args
    )
    if (
    !from!"std.traits".isCallable!F
    )
  2. auto ref invoke(Args args)

Parameters

F

the function to invoke.

args Args

the arguments to call F with.

Return Value

Type: auto ref

The value returned by F.

Examples

void method(ref Foo) {}

struct Foo { void method() {} }
Foo foo;

invoke!(Foo.method)(foo); // correctly invokes method from foo
invoke!method(foo);       // correctly invokes method
invoke!(_ => _ + 1)(4);   // correctly invokes the lambda

Meta