Package org.apache.groovy.runtime

# Class Comprehensions

java.lang.Object
org.apache.groovy.runtime.Comprehensions

@Incubating public final class Comprehensions extends Object
Runtime bind/map dispatcher for monadic comprehensions — the emit target of the `DO` macro.

The macro runs at `SEMANTIC_ANALYSIS`, before type checking, so it cannot know the carrier's bind-method name and cannot emit it directly. Instead it emits `Comprehensions.bind(carrier) { x -> ... }` calls; this class resolves the carrier-specific method at runtime (dynamic Groovy), while the `groovy.typecheckers.MonadicChecker` type-checking extension specialises this one signature under `@CompileStatic`.

This is runtime support invoked from generated bytecode, hence its placement in core alongside the rest of the Groovy runtime; the `DO` macro and the type checker are compile-time only and remain in their optional modules.

Participation is resolved first-match-wins:

1. standard allow-list (`MonadicCarrierRegistry`);
2. structural (`flatMap`/`map` present);
3. `@Monadic` opt-in (honours `bind`/`map` overrides).

A configured marker interface is a further opt-in mechanism that is not yet implemented.

Surface generosity: the carrier's bind method may accept a `Function` or a `Closure`; the closure is adapted to whichever the target declares. Monad laws are not enforced — structural participation, algebraic-law obligation on the implementer (the `@Reducer`/`@Associative` treatment).

Since:
  6.0.0

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static Object`
  `bind(Object carrier, Closure<?> fn)`
  Bind (flatMap-shaped): `carrier.<bind>(x -> fn(x))` where `fn` yields the same carrier.
  `static Object`
  `map(Object carrier, Closure<?> fn)`
  Map: `carrier.<map>(x -> fn(x))` where `fn` yields a plain value.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### bind

    public static Object bind(Object carrier, Closure<?> fn)
    Bind (flatMap-shaped): `carrier.<bind>(x -> fn(x))` where `fn` yields the same carrier.

  - ### map

    public static Object map(Object carrier, Closure<?> fn)
    Map: `carrier.<map>(x -> fn(x))` where `fn` yields a plain value.