-
Notifications
You must be signed in to change notification settings - Fork 0
/
Eq.scala
74 lines (61 loc) · 2.35 KB
/
Eq.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package equality.core
import annotation.implicitNotFound
export Eq.eqFromAssumed
export Eq.canEqualFromEq
/**
* Value equality type class with automatic derivation for `Product` types.
*
* @see [[https://github.com/antognini/type-safe-equality/blob/main/README.md#eq-type-class Library documentation]]
* @tparam T arbitrary type
*/
@implicitNotFound("Values of types ${T} and ${T} cannot be compared with == or !=")
sealed trait Eq[-T]:
/** For testing only. */
private[equality] val violations: Seq[String] = Nil
object Eq:
given canEqualFromEq[T: Eq]: CanEqual[T, T] = CanEqual.derived
given eqFromAssumed[A: Eq.assumed]: Eq[A] = EqAny
/**
* Creates an Eq instance for an arbitrary type without verifying equality requirements.
*
* @see [[https://github.com/antognini/type-safe-equality/blob/main/README.md#assuming-equality Library documentation]]
* @tparam T arbitrary type
*/
sealed trait assumed[-T] extends Eq[T]
/**
* Creates an Eq instance for an arbitrary type without verifying equality requirements.
*
* @see [[https://github.com/antognini/type-safe-equality/blob/main/README.md#assuming-equality Library documentation]]
*/
object assumed extends assumed[Any]:
def derived[T]: assumed[T] = assumed
/**
* Creates an Eq instance for a `Product` type while verifying equality requirements.
*
* @see [[https://github.com/antognini/type-safe-equality/blob/main/README.md#verifying-equality Library documentation]]
* @tparam T `Product` type
* @return Eq instance for the specified type
*/
inline def derived[T]: Eq[T] = EqMacro.derived
/**
* Identity function that requires an Eq type class instance for the passed argument.
*
* @see [[https://github.com/antognini/type-safe-equality/blob/main/README.md#assuming-equality Library documentation]]
* @tparam T arbitrary type
*/
def apply[T: Eq](value: T): T = value
private object EqAny extends Eq[Any]
private[equality] def apply[T]: Eq[T] = EqAny
/** For testing only. */
private[equality] def apply[T](testViolations: Seq[String]): Eq[T] =
new Eq[T]:
override val violations = testViolations
object Universal:
/**
* Enables universal equality.
*
* This is equivalent to the default behavior of `==` and `!=` operators in Scala.
*/
given scala_Any: Eq[Any] = Eq.assumed
end Universal
end Eq