dynamic<Param, Return> method

DynamicCapsule<Param, Return> dynamic<Param, Return>(
  1. Return dyn(
    1. CapsuleHandle,
    2. Param
    )
)

Shorthand for a fully formed dynamic capsule. See ExperimentalSideEffects.dynamic for more.

Basic usage:

final myDynamicCapsule = capsule.dynamic((use, Foo foo) {
  return use(barCapsule).useFoo(foo);
});

Warning

Due to limitations in Dart's type system, if you wish to use the created capsule recursively, you will need to explicitly write the return type like so:

final DynamicCapsule<int, BigInt> fibonacciCapsule =
    capsule.dynamic((use, int n) {
  return switch (n) {
    _ when n < 0 => throw ArgumentError.value(n),
    0 => BigInt.zero,
    1 => BigInt.one,
    _ => use(fibonacciCapsule[n - 1]) + use(fibonacciCapsule[n - 2]),
  };
});

NOTE: I'd recommend specifying the return type under all situations regardless, as it'll increase code reability.

Implementation

DynamicCapsule<Param, Return> dynamic<Param, Return>(
  Return Function(CapsuleHandle, Param) dyn,
) {
  return (CapsuleHandle use) => use.dynamic(dyn);
}