ENvironmentSet / hyogwa

Natural 🌿 effect system that fits TypeScript
https://stackblitz.com/edit/vitejs-vite-2fotdf?file=src%2Fmain.ts
MIT License
58 stars 2 forks source link

Stricter `handle` function #10

Open ENvironmentSet opened 1 year ago

ENvironmentSet commented 1 year ago

Currently, there's nothing at compile time preventing users from not calling handle tactics. I think we can add more restriction by forcing users to return special values which only produced by handle tactics. But I'm also concerning that this would make writing handlers little more complicated. Any thoughts?

type TacticIsCalled = unique symbol // some special type

interface HandleTactics<in ER, in R> {
  resume(value: ER): TacticIsCalled
  abort(value: R): TacticIsCalled
}

type Handlers<E extends Effects, R = never>
  = Simplify<
      UnionToIntersection<
        E extends Code<`${infer S}.${infer C}`, infer P, infer ER> ?
          Eq<P, never> extends false ?
            { [K in S]: { [K in C]: (...parameters: [...P, HandleTactics<ER, R>]) => TacticIsCalled | Generator<Effects, TacticIsCalled > } }
            : { [K in S]: { [K in C]: ER | Generator<Effects, ER> } }
          : never
      >
    >