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: Make Equal an implicit trait, namely remove the type Data.Data #2023

Merged
merged 1 commit into from
Feb 2, 2024

Conversation

mikearnaldi
Copy link
Member

@mikearnaldi mikearnaldi commented Jan 31, 2024

With this change we remove the Data.Data type and we make Equal.Equal & Hash.Hash an implicit traits.

The main reason is that Data.Data<A> was structurally equivalent to A & Equal.Equal but extending Equal.Equal doesn't mean that the equality is implemented by-value, so the type was simply adding noice without gaining any level of safety.

The module Data remains unchanged at the value level, all the functions priorly available are supposed to work in exactly the same manner.

At the type level instead the functions return Readonly variants, so for example we have:

import { Data } from "effect";

const obj = Data.struct({
  a: 0,
  b: 1,
});

will have the obj typed as:

declare const obj: {
  readonly a: number;
  readonly b: number;
};

closes #1962

Copy link

changeset-bot bot commented Jan 31, 2024

🦋 Changeset detected

Latest commit: 3cfcd29

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 Major
effect Minor
@effect/schema Major
@effect/cli Major
@effect/rpc Major
@effect/experimental Major
@effect/platform-browser Major
@effect/platform-bun Major
@effect/platform-node-shared Major
@effect/platform-node Major
@effect/rpc-http-node Major
@effect/rpc-http Major
@effect/rpc-nextjs Major
@effect/rpc-workers Major
@effect/opentelemetry 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

@github-actions github-actions bot changed the base branch from main to next-minor January 31, 2024 18:00
@mikearnaldi mikearnaldi changed the title feat: Make Equal an implicit trait, namely remove the type Data.Data Make Equal an implicit trait, namely remove the type Data.Data Jan 31, 2024
@mikearnaldi mikearnaldi changed the title Make Equal an implicit trait, namely remove the type Data.Data feat: Make Equal an implicit trait, namely remove the type Data.Data Jan 31, 2024
fubhy
fubhy previously requested changes Jan 31, 2024
.changeset/mighty-donuts-behave.md Outdated Show resolved Hide resolved
.changeset/mighty-donuts-behave.md Outdated Show resolved Hide resolved
.changeset/mighty-donuts-behave.md Outdated Show resolved Hide resolved
@datner
Copy link
Contributor

datner commented Jan 31, 2024

There is a pitfall in this though, users might unwittingly squander the Data behavior and be non the wiser

pipe(
  ReadonlyArray.makeBy(10, () => new A()),
  Data.array,
  Effect.forEach(_ => Effect.as(notifyMikeAboutA(_), _))
  Effect.filterOrDie(
    Equal.equals(Data.array(ReadonlyArray.makeBy(10, () => new A()))),
    () => "w00t"
  )
) // -> it dies with w00t

@mikearnaldi
Copy link
Member Author

mikearnaldi commented Jan 31, 2024

There is a pitfall in this though, users might unwittingly squander the Data behavior and be non the wiser

pipe(
  ReadonlyArray.makeBy(10, () => new A()),
  Data.array,
  Effect.forEach(_ => Effect.as(notifyMikeAboutA(_), _))
  Effect.filterOrDie(
    Equal.equals(Data.array(ReadonlyArray.makeBy(10, () => new A()))),
    () => "w00t"
  )
) // -> it dies with w00t

That's correct as forEach returns a new copy of the array which is mutable, I guess it is a trade-off, we could also make Data.array idempotent, so that if you want to be really sure it compares correctly you can just always call it

@datner
Copy link
Contributor

datner commented Jan 31, 2024

That's correct as forEach returns a new copy of the array which is mutable, I guess it is a trade-off, we could also make Data.array idempotent, so that if you want to be really sure it compares correctly you can just always call it

I can't come up with a solution for this pitfall, I guess we will just have to make sure to note it in the docs.

@fubhy
Copy link
Member

fubhy commented Feb 1, 2024

I can't find any more typos 👎

@mikearnaldi mikearnaldi dismissed fubhy’s stale review February 2, 2024 09:56

Requested changes are done

@mikearnaldi mikearnaldi merged commit 6d49dd3 into next-minor Feb 2, 2024
9 checks passed
@mikearnaldi mikearnaldi deleted the feat/make-equal-implicit branch February 2, 2024 10:16
@github-actions github-actions bot mentioned this pull request Feb 2, 2024
@datner
Copy link
Contributor

datner commented Feb 6, 2024

@mikearnaldi hmmm the bot (and @/gcanti 😃 ) is quite noisy here, is this a one-time or will all updates be this noisy until they publish?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

4 participants