Skip to content

Commit

Permalink
simplify EffectGenerator type (#2611)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Apr 25, 2024
1 parent ba67a5b commit 6222404
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-snakes-occur.md
@@ -0,0 +1,5 @@
---
"effect": patch
---

simplify EffectGenerator type to improve inference
11 changes: 4 additions & 7 deletions packages/effect/src/Effect.ts
Expand Up @@ -101,11 +101,8 @@ export interface Effect<out A, out E = never, out R = never> extends Effect.Vari
* @since 3.0.0
* @category models
*/
export interface EffectGenerator<Eff extends Effect<any, any, any>> {
next(...args: ReadonlyArray<any>): IteratorResult<Eff, Effect.Success<Eff>>
return(value: Effect.Success<Eff>): IteratorResult<Eff, Effect.Success<Eff>>
throw(e: any): IteratorResult<Eff, Effect.Success<Eff>>
[Symbol.iterator](): EffectGenerator<Eff>
export interface EffectGenerator<T extends Effect<any, any, any>> {
next(...args: ReadonlyArray<any>): IteratorResult<T, Effect.Success<T>>
}

/**
Expand Down Expand Up @@ -1123,15 +1120,15 @@ export const dieSync: (evaluate: LazyArg<unknown>) => Effect<never> = core.dieSy
*/
export const gen: {
<Eff extends Effect<any, any, any>, AEff>(
f: (resume: Adapter) => Generator<Eff, AEff, any>
f: (resume: Adapter) => Generator<Eff, AEff, never>
): Effect<
AEff,
[Eff] extends [never] ? never : [Eff] extends [Effect<infer _A, infer E, infer _R>] ? E : never,
[Eff] extends [never] ? never : [Eff] extends [Effect<infer _A, infer _E, infer R>] ? R : never
>
<Self, Eff extends Effect<any, any, any>, AEff>(
self: Self,
f: (this: Self, resume: Adapter) => Generator<Eff, AEff, any>
f: (this: Self, resume: Adapter) => Generator<Eff, AEff, never>
): Effect<
AEff,
[Eff] extends [never] ? never : [Eff] extends [Effect<infer _A, infer E, infer _R>] ? E : never,
Expand Down
2 changes: 1 addition & 1 deletion packages/effect/src/Either.ts
Expand Up @@ -713,7 +713,7 @@ export const gen: Gen.Gen<EitherTypeLambda, Gen.Adapter<EitherTypeLambda>> = (f)
return current
}
while (!state.done) {
state = iterator.next(current.right)
state = iterator.next(current.right as never)
if (!state.done) {
current = state.value
if (isLeft(current)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/effect/src/Option.ts
Expand Up @@ -1276,7 +1276,7 @@ export const gen: Gen.Gen<OptionTypeLambda, Gen.Adapter<OptionTypeLambda>> = (f)
return current
}
while (!state.done) {
state = iterator.next(current.value)
state = iterator.next(current.value as never)
if (!state.done) {
current = state.value
if (isNone(current)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/effect/src/STM.ts
Expand Up @@ -1065,7 +1065,7 @@ export interface Adapter {
* @category constructors
*/
export const gen: <Eff extends STM<any, any, any>, AEff>(
f: (resume: Adapter) => Generator<Eff, AEff, any>
f: (resume: Adapter) => Generator<Eff, AEff, never>
) => STM<
AEff,
[Eff] extends [never] ? never : [Eff] extends [STM<infer _A, infer E, infer _R>] ? E : never,
Expand Down
2 changes: 1 addition & 1 deletion packages/effect/src/Utils.ts
Expand Up @@ -174,7 +174,7 @@ export interface Variance<in out F extends TypeLambda, in R, out O, out E> {
*/
export interface Gen<F extends TypeLambda, Z> {
<K extends Variance<F, any, any, any> | Kind<F, any, any, any, any>, A>(
body: (resume: Z) => Generator<K, A>
body: (resume: Z) => Generator<K, A, never>
): Kind<
F,
[K] extends [Variance<F, infer R, any, any>] ? R : [K] extends [Kind<F, infer R, any, any, any>] ? R : never,
Expand Down
2 changes: 1 addition & 1 deletion packages/effect/src/internal/stm/stm.ts
Expand Up @@ -626,7 +626,7 @@ export const gen: typeof STM.gen = (f) =>
): STM.STM<any, any, any> =>
state.done ?
core.succeed(state.value) :
core.flatMap(state.value, (val: any) => run(iterator.next(val)))
core.flatMap(state.value, (val: any) => run(iterator.next(val as never)))
return run(state)
})

Expand Down

0 comments on commit 6222404

Please sign in to comment.