Differences Between `Effect.Tag` and `Context.Tag`

is there a deeper difference between Effect.Tag and Context.Tag other than Effect.Tag creates static properties for the fields which you can directly yield*?

class NotificationsEffect extends Effect.Tag("NotificationsEffect")<
  NotificationsEffect,
  { readonly notify: (message: string) => Effect.Effect<void> }
>() {
  static Live = Layer.succeed(this, {
    notify: (message) => Console.log(message),
  });
}

class NotificationsContext extends Context.Tag("NotificationsContext")<
  NotificationsContext,
  { readonly notify: (message: string) => Effect.Effect<void> }
>() {
  static Live = Layer.succeed(this, {
    notify: (message) => Console.log(message),
  });
}

async function main() {
  const runtime = ManagedRuntime.make(
    Layer.mergeAll(NotificationsEffect.Live, NotificationsContext.Live)
  );

  await runtime.runPromise(NotificationsEffect.notify("Hello, world!"));
  await runtime.runPromise(
    Effect.gen(function* () {
      const notificationsContext = yield* NotificationsContext;
      yield* notificationsContext.notify("Hello, world!");
    })
  );

  await runtime.dispose();
}

main().then(() => console.log("Program completed successfully."));
Was this page helpful?