Skip to content

Commit 55fe672

Browse files
committed
chore: improve object equality, fix eval and tag
1 parent 45bece9 commit 55fe672

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

.changeset/weak-melons-ring.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tsplus/stdlib": patch
3+
---
4+
5+
Improve Equals on Object, fix Tag/Eval equality

packages/stdlib/_src/io/Eval/definition.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,33 +49,57 @@ export function unifyEval<X extends Eval<any>>(
4949
}
5050

5151
export interface Succeed<A> extends Eval<A> {}
52-
export class Succeed<A> {
52+
export class Succeed<A> implements Equals {
5353
readonly _tag = "Succeed"
5454

5555
readonly [EvalSym]: EvalSym = EvalSym
5656
readonly [_A]!: () => A
5757

5858
constructor(readonly a: Lazy<A>) {}
59+
60+
[Equals.sym](that: unknown) {
61+
return this === that
62+
}
63+
64+
[Hash.sym]() {
65+
return Hash.randomCached(this)
66+
}
5967
}
6068

6169
export interface Suspend<A> extends Eval<A> {}
62-
export class Suspend<A> {
70+
export class Suspend<A> implements Equals {
6371
readonly _tag = "Suspend"
6472

6573
readonly [EvalSym]: EvalSym = EvalSym
6674
readonly [_A]!: () => A
6775

6876
constructor(readonly f: Lazy<EvalInternal<A>>) {}
77+
78+
[Equals.sym](that: unknown) {
79+
return this === that
80+
}
81+
82+
[Hash.sym]() {
83+
return Hash.randomCached(this)
84+
}
6985
}
7086

7187
export interface FlatMap<A, B> extends Eval<B> {}
72-
export class FlatMap<A, B> {
88+
export class FlatMap<A, B> implements Equals {
7389
readonly _tag = "FlatMap"
7490

7591
readonly [EvalSym]: EvalSym = EvalSym
7692
readonly [_A]!: () => A
7793

7894
constructor(readonly value: EvalInternal<A>, readonly cont: (a: A) => EvalInternal<B>) {}
95+
96+
[Equals.sym](that: unknown) {
97+
return this === that
98+
}
99+
100+
[Hash.sym]() {
101+
return Hash.randomCached(this)
102+
}
79103
}
80104

81105
export interface EvalF extends HKT {

packages/stdlib/_src/service/Tag.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ export const Tag: TagOps = Object.assign(
1414
[Tag.sym]: identity,
1515
toEnv(value) {
1616
return Env(this, value)
17+
},
18+
[Equals.sym](that) {
19+
return this === that
20+
},
21+
[Hash.sym]() {
22+
return Hash.randomCached(this)
1723
}
1824
}),
1925
{
@@ -25,7 +31,7 @@ export const Tag: TagOps = Object.assign(
2531
/**
2632
* @tsplus type Tag
2733
*/
28-
export interface Tag<in out S> {
34+
export interface Tag<in out S> extends Equals {
2935
readonly [Tag.sym]: (_: S) => S
3036

3137
toEnv(value: S): Service.Env<S>

packages/stdlib/_src/structure/Equals.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ const protoMap = new Map<any, (a: any, b: any) => boolean>([
5959
[
6060
Object.prototype,
6161
(a: object, b: object) => {
62+
if ("_tag" in a) {
63+
if ("_tag" in b) {
64+
if (a["_tag"] !== b["_tag"]) {
65+
return false
66+
}
67+
} else {
68+
return false
69+
}
70+
}
6271
const keysA = Object.keys(a).sort()
6372
const keysB = Object.keys(b).sort()
6473
if (keysA.length !== keysB.length) {

0 commit comments

Comments
 (0)