dynamicCapsule<Param, Return> function

Capsule<DynamicCapsule<Param, Return>> dynamicCapsule<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 = dynamicCapsule((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 Capsule<DynamicCapsule<int, BigInt>> fibonacciCapsule =
    dynamicCapsule((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

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