# Remove auto-flattening ## Summary Remove runtime `isEffect` auto-flattening from all public APIs. Callbacks must return `Effect` explicitly; plain values are no longer silently wrapped or ignored at runtime. ## Background Many functions across Effect, Schedule, Stream, Sink, and Channel accept callbacks whose return type is `X | Effect`. At runtime they call `isEffect` to decide whether to flatten or wrap the result. This creates: - **Inconsistency** — `Effect.tap` auto-flattens but `Effect.tapError`, `Effect.tapCause`, etc. do not (issue #849). - **Hidden behavior** — a callback returning a non-Effect value (e.g. `Promise`) is silently discarded or wrapped, masking bugs. - **Type complexity** — conditional types like `[X] extends [Effect<…>] ? … : …` inflate overload surfaces. The fix is to require `Effect` everywhere and remove the runtime dispatch. ## Goals - Remove all runtime `isEffect` value-flattening checks from callback-based APIs. - Simplify type signatures: remove conditional-type overloads and `| Effect` unions on callback return types. - Remove the `{ onlyEffect: true }` option from `Effect.tap` (it becomes the only behavior). - Keep `dual()` discrimination uses of `isEffect` — those distinguish data-first from data-last and are unrelated to auto-flattening. ## Non-goals - No changes to `Effect.fn` generator-vs-Effect discrimination (it serves a different purpose: supporting both `gen` iterators and plain Effect returns). - No changes to `dual()` discrimination logic that uses `isEffect` to detect data-first vs data-last invocations. ## Affected functions ### Effect module | Function | Current callback return type | Change | | --------------------- | ------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------- | | `andThen` | `X` (conditional type resolves `Effect` or `Effect`) | Require `Effect` return; remove conditional-type overloads and `NotFunction` overloads | | `tap` | `X` (conditional type) + `{ onlyEffect: true }` overloads | Require `Effect` return; remove conditional-type, `NotFunction`, and `onlyEffect` overloads | | `onExit` | `Effect \| void` | Require `Effect` | | `onExitInterruptible` | `Effect \| void` | Require `Effect` | | `onInterrupt` | `Effect \| ((interruptors) => Effect)` | Require `(interruptors) => Effect`; drop the bare-Effect overload | | `when` | `LazyArg \| Effect` | Require `Effect` only | | `acquireUseRelease` | Internal impl types release as `Effect \| void` | Align internal impl with public sig (already `Effect`) | ### Schedule module | Function | Current callback return type | Change | | ------------- | ---------------------------------------- | ------------------------------- | | `addDelay` | `DurationInput \| Effect` | Require `Effect` | | `map` | `Output2 \| Effect` | Require `Effect` | | `modifyDelay` | `DurationInput \| Effect` | Require `Effect` | | `reduce` | `State \| Effect` | Require `Effect` | | `unfold` | `State \| Effect` | Require `Effect` | | `while` | `boolean \| Effect` | Require `Effect` | ### Stream module | Function | Current callback return type | Change | | ---------- | ------------------------------------------ | -------------------------------- | | `paginate` | `[A, Option] \| Effect<[A, Option]>` | Require `Effect<[A, Option]>` | | `when` | `LazyArg \| Effect` | Require `Effect` | ### Sink module | Function | Current callback return type | Change | | ----------- | ---------------------------- | ------------------- | | `fold` | `S \| Effect` | Require `Effect` | | `foldArray` | `S \| Effect` | Require `Effect` | | `foldUntil` | `S \| Effect` | Require `Effect` | ### Channel module | Function | Current callback return type | Change | | ---------------------- | ---------------------------- | ------------------------ | | `asyncQueue` (private) | `void \| Effect` | Require `Effect` | | `mapInput` | `InElem \| Effect` | Require `Effect` | | `mapInputError` | `InErr \| Effect` | Require `Effect` | ### Internal | Function | Current callback return type | Change | | -------------------- | -------------------------------------------- | --------------------------------- | | `request` (internal) | `RequestResolver \| Effect` | Require `Effect` | ## Requirements ### `Effect.andThen` **Type signature — before:** ```ts export const andThen: { ( f: (a: A) => X ): ( self: Effect ) => [X] extends [Effect] ? Effect : Effect ( f: NotFunction ): ( self: Effect ) => [X] extends [Effect] ? Effect : Effect // + data-first variants } ``` **Type signature — after:** ```ts export const andThen: { (f: (a: A) => Effect): (self: Effect) => Effect (self: Effect, f: (a: A) => Effect): Effect } ``` **Runtime — before:** ```ts flatMap(self, (a) => { if (isEffect(f)) return f const value = typeof f === "function" ? internalCall(() => f(a)) : f return isEffect(value) ? value : succeed(value) }) ``` **Runtime — after:** ```ts flatMap(self, (a) => internalCall(() => f(a))) ``` ### `Effect.tap` **Type signature — after:** ```ts export const tap: { (f: (a: NoInfer) => Effect): (self: Effect) => Effect (self: Effect, f: (a: NoInfer) => Effect): Effect } ``` Remove `NotFunction` overloads, conditional-type overloads, and `{ onlyEffect: true }` overloads. **Runtime — after:** ```ts flatMap(self, (a) => as(internalCall(() => f(a)), a)) ``` ### `Effect.onExit` / `Effect.onExitInterruptible` Remove `| void` from the finalizer return type. The `onExitPrimitive` implementation drops the `isEffect(eff) ? flatMap(eff, …) : exit` branch and always `flatMap`s. ### `Effect.onInterrupt` Remove the bare-Effect overload. Always require a function `(interruptors: ReadonlySet) => Effect`. Drop the `isEffect(finalizer) ? constant(finalizer) : finalizer` dispatch. ### `Effect.when` Remove `LazyArg` from the union. Require `Effect`. Drop the `isEffect(condition) ? condition : sync(condition)` dispatch. ### Schedule, Stream, Sink, Channel functions For each function listed in the tables above: 1. Remove the `| Effect<…>` or `| plainValue` union from the callback return type, keeping only `Effect<…>`. 2. Remove the runtime `isEffect(result)` branch — always treat the return as an `Effect`. 3. Update internal callers to wrap plain values in `Effect.succeed` or `Effect.sync` where needed. ## Migration Callers currently relying on auto-flattening must wrap return values: | Before | After | | -------------------------------------------- | ------------------------------------------------------------ | | `Effect.andThen(self, 42)` | `Effect.andThen(self, () => Effect.succeed(42))` | | `Effect.andThen(self, (a) => a + 1)` | `Effect.andThen(self, (a) => Effect.succeed(a + 1))` | | `Effect.tap(self, (a) => console.log(a))` | `Effect.tap(self, (a) => Effect.sync(() => console.log(a)))` | | `Effect.tap(self, Console.log("hi"))` | `Effect.tap(self, () => Console.log("hi"))` | | `Effect.tap(self, f, { onlyEffect: true })` | `Effect.tap(self, f)` | | `Effect.when(self, () => true)` | `Effect.when(self, Effect.succeed(true))` | | `Effect.onExit(self, (exit) => { … })` | `Effect.onExit(self, (exit) => Effect.sync(() => { … }))` | | `Schedule.addDelay(sched, () => "1 second")` | `Schedule.addDelay(sched, () => Effect.succeed("1 second"))` | | `Sink.fold(zero, cont, (s, i) => s + i)` | `Sink.fold(zero, cont, (s, i) => Effect.succeed(s + i))` | ## Implementation order 1. **`Effect.andThen`** and **`Effect.tap`** — highest visibility; update type signatures, remove conditional-type overloads, simplify runtime. 2. **`Effect.onExit`**, **`Effect.onExitInterruptible`**, **`Effect.acquireUseRelease`** — remove `| void` from finalizer types and `isEffect` branches in `onExitPrimitive`. 3. **`Effect.onInterrupt`** — require function form only. 4. **`Effect.when`** — require `Effect`. 5. **Schedule functions** — `addDelay`, `map`, `modifyDelay`, `reduce`, `unfold`, `while`. 6. **Stream functions** — `paginate`, `when`. 7. **Sink functions** — `fold`, `foldArray`, `foldUntil`. 8. **Channel functions** — `mapInput`, `mapInputError`, `asyncQueue`. 9. **Internal** — `request`. 10. Fix all internal callers and tests that relied on auto-flattening. ## Testing - Update existing tests that pass plain values to auto-flattening APIs. - Verify that passing a non-Effect to `andThen`/`tap` is a compile error. - Run `pnpm test` for each affected package after changes. ## Validation - `pnpm lint-fix` - `pnpm test ` - `pnpm check` (run `pnpm clean` then re-run if it fails) - `pnpm build` - `pnpm docgen`