Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(breaking): Add ClassTag, improve Effect.serviceConstants, require string identifier for all tags #2028

Merged
merged 4 commits into from
Feb 3, 2024

Conversation

mikearnaldi
Copy link
Member

@mikearnaldi mikearnaldi commented Feb 2, 2024

With this change we now require a string key to be provided for all tags and renames the dear old Tag to GenericTag, so when previously you could do:

import { Effect, Context } from "effect";
interface Service {
  readonly _: unique symbol;
}
const Service = Context.Tag<
  Service,
  {
    number: Effect.Effect<never, never, number>;
  }
>();

you are now mandated to do:

import { Effect, Context } from "effect";
interface Service {
  readonly _: unique symbol;
}
const Service = Context.GenericTag<
  Service,
  {
    number: Effect.Effect<never, never, number>;
  }
>("Service");

This makes by default all tags globals and ensures better debuggaility when unexpected errors arise.

Furthermore we introduce a new way of constructing tags that should be considered the new default:

import { Effect, Context } from "effect";
class Service extends Context.Tag("Service")<
  Service,
  {
    number: Effect.Effect<never, never, number>;
  }
>() {}

const program = Effect.flatMap(Service, ({ number }) => number).pipe(
  Effect.flatMap((_) => Effect.log(`number: ${_}`))
);

this will use "Service" as the key and will create automatically an opaque identifier (the class) to be used at the type level, it does something similar to the above in a single shot.

Copy link

changeset-bot bot commented Feb 2, 2024

🦋 Changeset detected

Latest commit: c74d900

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 18 packages
Name Type
@effect/platform-node-shared Major
@effect/platform-browser Major
@effect/opentelemetry Major
@effect/platform-node Major
@effect/experimental Major
@effect/platform-bun Major
@effect/rpc-workers Major
@effect/platform Major
@effect/rpc-http Major
effect Minor
@effect/schema Major
@effect/cli Major
@effect/rpc Major
@effect/rpc-http-node Major
@effect/rpc-nextjs Major
@effect/printer-ansi Major
@effect/printer Major
@effect/typeclass Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@patroza
Copy link
Member

patroza commented Feb 2, 2024

Love the class. But then go all the way; allow to take the name of the class?
nvm, as the const key is what makes the unique identification work it's not useful.

@mikearnaldi
Copy link
Member Author

As a follow up I propose renaming Tag to GenericTag and moving ClassTag to be Tag

@patroza
Copy link
Member

patroza commented Feb 2, 2024

As a follow up I propose renaming Tag to GenericTag and moving ClassTag to be Tag

Some more ideas to infer the shape from the make effect; https://github.com/effect-ts-app/libs/blob/34c0458a3e9e5a42b723260802785ce369ee07d5/packages/prelude/_src/service.ts#L72

@mikearnaldi
Copy link
Member Author

Love the class. But then go all the way; allow to take the name of the class?

Not sure I understand your suggestion, please provide a code snippet

@IMax153
Copy link
Member

IMax153 commented Feb 2, 2024

@mikearnaldi - big fan of this - simplifies a lot of boilerplate around Tag. I also agree with Tag -> GenericTag and ClassTag -> Tag.

Nice work 👍

@tim-smart
Copy link
Member

Looks good! Agree with the renaming too.

Quick fix to ensure the definition stack is set correctly: #2029

@mikearnaldi mikearnaldi merged commit a21be5a into next-minor Feb 3, 2024
9 checks passed
@mikearnaldi mikearnaldi deleted the feat/new-tag branch February 3, 2024 18:52
@github-actions github-actions bot mentioned this pull request Feb 3, 2024
github-actions bot pushed a commit that referenced this pull request Feb 3, 2024
…e string identifier for all tags (#2028)

Co-authored-by: Tim <hello@timsmart.co>
github-actions bot pushed a commit that referenced this pull request Feb 4, 2024
…e string identifier for all tags (#2028)

Co-authored-by: Tim <hello@timsmart.co>
github-actions bot pushed a commit that referenced this pull request Feb 4, 2024
…e string identifier for all tags (#2028)

Co-authored-by: Tim <hello@timsmart.co>
github-actions bot pushed a commit that referenced this pull request Feb 4, 2024
…e string identifier for all tags (#2028)

Co-authored-by: Tim <hello@timsmart.co>
github-actions bot pushed a commit that referenced this pull request Feb 4, 2024
…e string identifier for all tags (#2028)

Co-authored-by: Tim <hello@timsmart.co>
github-actions bot pushed a commit that referenced this pull request Feb 5, 2024
…e string identifier for all tags (#2028)

Co-authored-by: Tim <hello@timsmart.co>
gcanti pushed a commit that referenced this pull request Feb 5, 2024
…e string identifier for all tags (#2028)

Co-authored-by: Tim <hello@timsmart.co>
tim-smart added a commit that referenced this pull request Feb 6, 2024
…e string identifier for all tags (#2028)

Co-authored-by: Tim <hello@timsmart.co>
github-actions bot pushed a commit that referenced this pull request Feb 6, 2024
…e string identifier for all tags (#2028)

Co-authored-by: Tim <hello@timsmart.co>
github-actions bot pushed a commit that referenced this pull request Feb 7, 2024
…e string identifier for all tags (#2028)

Co-authored-by: Tim <hello@timsmart.co>
tim-smart added a commit that referenced this pull request Feb 7, 2024
…e string identifier for all tags (#2028)

Co-authored-by: Tim <hello@timsmart.co>
github-actions bot pushed a commit that referenced this pull request Feb 7, 2024
…e string identifier for all tags (#2028)

Co-authored-by: Tim <hello@timsmart.co>
github-actions bot pushed a commit that referenced this pull request Feb 7, 2024
…e string identifier for all tags (#2028)

Co-authored-by: Tim <hello@timsmart.co>
github-actions bot pushed a commit that referenced this pull request Feb 7, 2024
…e string identifier for all tags (#2028)

Co-authored-by: Tim <hello@timsmart.co>
tim-smart added a commit that referenced this pull request Feb 9, 2024
…e string identifier for all tags (#2028)

Co-authored-by: Tim <hello@timsmart.co>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

4 participants