Skip to content

Commit

Permalink
Add full Eq example
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Aug 19, 2024
1 parent 2f1abec commit 0125d9f
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions doc/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ object Size:
given Size[Boolean] = new Size[Boolean]:
def size(value: Boolean): Double = 1.0

given Size[Char] with
given Size[Char]:
def size(value: Char): Double = 2.0

given Size[String] = _.length.toDouble
Expand Down Expand Up @@ -378,7 +378,7 @@ object Eq extends ProductDerivation[Eq]:
(left, right) =>
fields(left):
[FieldType] => leftField =>
context.eq(leftField, complement(right))
context.equal(leftField, complement(right))
.foldLeft(true)(_ && _)
```

Expand Down Expand Up @@ -543,6 +543,25 @@ The interpretation of this implementation is that if the left and right sum type
then we use `context`, the typeclass instance that is common to both, to compare them. Otherwise, since they are
evidently different, we return `false`.

Therefore, a complete implementation of `Eq` is as simple as:
```scala
trait Eq[ValueType]:
def equal(left: ValueType, right: ValueType): Boolean

object Eq extends ProductDerivation[Eq]:
inline def join[DerivationType <: Product: ProductReflection]: Eq[DerivationType] =
(left, right) =>
fields(left):
[FieldType] => left => context.equal(left, complement(right))
.foldLeft(true)(_ && _)

inline def split[DerivationType: SumReflection]: Eq[DerivationType] =
(left, right) =>
variant(left):
[VariantType <: DerivationType] => left =>
complement(right).let(context.equal(left, _)).or(false)
```

#### Producer Sum Typeclasses

As with the `construct` method for product types, the `delegate` method is used for producer sum types which
Expand Down

0 comments on commit 0125d9f

Please sign in to comment.