You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Introduces a generator for lazy for-comprehension variants that produce intermediate `ForLazyN<M>` classes for supported monadic types (e.g., _Option_, _Try_, _Either_, etc.).
This addition complements the existing eager for-comprehension generator by introducing a lazy variant.
- Added generation of `ForLazyN<M`> classes (arity 2 to N) that:
- Use one monadic source (ts1) followed by a chain of dependent functions (ts2, ts3, …).
- Allow lazy composition of monadic operations through nested lambdas.
- Added corresponding `For(...)` factory methods for convenient construction.
- Implemented a recursively generated `yield(...)` method with proper indentation and block structure for readability.
The generator handles mixed monadic/function parameter patterns such as:
```java
Option<T1> ts1;
Function1<T1, Option<T2>> ts2;
Function2<T1, T2, Option<T3>> ts3;
```
expands for-comprehension with lazily-evaluated variants, so that the existing is possible:
```java
Option<Integer> result = API.For(
calculate1(),
r1 -> calculate2(r1),
(r1, r2) -> calculate3(r1, r2)).yield((r1, r2, r3) -> r1 + r2 + r3);
```
The generator enforces that the first argument is always a monadic value, not a function. This is necessary because:
- The first argument acts as the entry point of the comprehension; it provides the initial context for all later `flatMap` calls.
- If _ts1_ were also a function (e.g., `Function0<M<T1>>`), **Java’s generic type inference fails to resolve chained type variables** properly due to the presence of methods with similar erasure.
- Subsequent arguments can safely be functions (`FunctionN<T1, ..., TN, M<T(N+1)>>`), since their input types are already determined by previous monadic values.
----
related: #3038
0 commit comments