Effect CommunityEC
Effect Community2y ago
16 replies
ingun37

How to make Service/Context generic?

I want to add a type parameter to the Context. For example, I’ll take code directly from the tutorial

import { Effect, Context } from "effect"
 
class Random extends Context.Tag("MyRandomService")<
  Random,
  { readonly next: Effect.Effect<number> }
>() {}


But what if I want to parameterize the output type of next()?


import { Effect, Context } from "effect"
 
class Random extends Context.Tag("MyRandomService")<
  Random,
  { readonly next: Effect.Effect<T> } // 👈 HOW??
>() {}


Guideline says avoid X/Y questions so I won’t post my attempts, but I basically tried class Random<T> and function<T>(){ return class …} style but they didn’t work 😭
Was this page helpful?