Skip to content

Commit

Permalink
version 0.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
antognini committed Jul 19, 2023
1 parent 6b56d4c commit 714c562
Show file tree
Hide file tree
Showing 54 changed files with 1,019 additions and 851 deletions.
387 changes: 246 additions & 141 deletions README.md

Large diffs are not rendered by default.

70 changes: 34 additions & 36 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
// Project
val projectName = "type-safe-equality"
ThisBuild / organization := "ch.produs"
ThisBuild / organizationName := "produs ag"
ThisBuild / organizationHomepage := None
ThisBuild / description := "Scala 3 type safe equality"
ThisBuild / homepage := Some(url("https://github.com/antognini/type-safe-equality"))
ThisBuild / homepage := Some(url(s"https://github.com/antognini/$projectName"))
ThisBuild / licenses := Seq("Apache-2.0" -> url("https://www.apache.org/licenses/LICENSE-2.0"))
ThisBuild / version := "0.5.0"
ThisBuild / scalaVersion := "3.3.0"
ThisBuild / version := "0.6.0"
ThisBuild / versionScheme := Some("semver-spec")
ThisBuild / publishTo := sonatypePublishToBundle.value
ThisBuild / scmInfo := Some(
ScmInfo(
url("https://github.com/antognini/type-safe-equality"),
"scm:[email protected]:antognini/type-safe-equality.git"
))
ThisBuild / developers := List(
Developer(
id = "LA",
name = "Luigi Antognini",
email = "",
url = url("https://github.com/antognini")
))
)
)
ThisBuild / scmInfo := Some(
ScmInfo(
url(s"https://github.com/antognini/$projectName"),
s"scm:[email protected]:antognini/$projectName.git"
)
)
Global / onChangedBuildSource := ReloadOnSourceChanges

lazy val compileOptions = Seq(

// Compile
ThisBuild / scalaVersion := "3.3.0"
ThisBuild / Compile / scalacOptions := Seq(
"-encoding", "utf8",
"-deprecation",
"-language:scala3",
Expand All @@ -33,34 +38,26 @@ lazy val compileOptions = Seq(
// "-Wunused:all"
)

lazy val root = project
.in(file("."))
.dependsOn(eq)
.settings(
name := "type-safe-equality",
publish / skip := true,
Compile / scalacOptions ++= compileOptions
)
.aggregate(eq, examples)

lazy val eq = project
.in(file("eq"))
.settings(
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "3.2.16" % "test"
),
name := "type-safe-equality",
Compile / scalacOptions ++= compileOptions
)
lazy val root = project.in(file(".")).dependsOn(equality).settings(
name := projectName,
publish / skip := true
).aggregate(equality, examples)

lazy val examples = project
.in(file("examples"))
.dependsOn(eq)
.settings(
publish / skip := true,
Compile / scalacOptions ++= compileOptions,
Compile / scalacOptions += "-Yimports:scala,scala.Predef,java.lang,equality.all"
lazy val equality = project.in(file("equality")).settings(
publishLocal := publishLocal.dependsOn(clean, Test / test).value,
publish := publish.dependsOn(clean, Test / test).value,
name := projectName,
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "3.2.16" % "test"
)
)

lazy val examples = project.in(file("examples")).dependsOn(equality).settings(
publish / skip := true,
Compile / scalacOptions += "-Yimports:scala,scala.Predef,java.lang,equality"
)


// Publish
sonatypeRepository := "https://s01.oss.sonatype.org/service/local"
Expand All @@ -79,3 +76,4 @@ credentials ++= Seq(
Option(System.getenv("SONATYPE_PASSWORD")).getOrElse("")
)
)
ThisBuild / publishTo := sonatypePublishToBundle.value
File renamed without changes.
118 changes: 118 additions & 0 deletions doc/StandardInstances.md

Large diffs are not rendered by default.

Binary file added doc/example-ide-1k.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 0 additions & 10 deletions eq/src/main/scala/equality/CheckStrictEqualityBuild.scala

This file was deleted.

27 changes: 0 additions & 27 deletions eq/src/main/scala/equality/all/Exports.scala

This file was deleted.

14 changes: 0 additions & 14 deletions eq/src/main/scala/equality/java_io/EqInstances.scala

This file was deleted.

10 changes: 0 additions & 10 deletions eq/src/main/scala/equality/java_security/EqInstances.scala

This file was deleted.

9 changes: 0 additions & 9 deletions eq/src/main/scala/equality/scala_concurrent/EqInstances.scala

This file was deleted.

9 changes: 0 additions & 9 deletions eq/src/main/scala/equality/universal/EqInstances.scala

This file was deleted.

13 changes: 0 additions & 13 deletions eq/src/test/scala/equality/TypeNameUtil.scala

This file was deleted.

31 changes: 31 additions & 0 deletions equality/src/main/scala/equality/Exports.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package equality

export equality.core.Eq
export equality.core.Eq.canEqualFromEq
export equality.core.Eq.eqFromAssumed

export equality.core.EqRef
export equality.core.EqRef.Operators.*

export equality.scala_.{AnyNumber, AnyJavaNumber}
export equality.scala_.Instances.given
export equality.scala_collection.Instances.given
export equality.scala_collection.CollectionExtension.*
export equality.scala_concurrent.Instances.given
export equality.scala_concurrent_duration.Instances.given
export equality.scala_io.Instances.given
export equality.scala_math.Instances.given
export equality.scala_util.Instances.given

export equality.java_io.Instances.given
export equality.java_lang.Instances.given
export equality.java_math.Instances.given
export equality.java_net.Instances.given
export equality.java_nio.Instances.given
export equality.java_nio_charset.Instances.given
export equality.java_nio_file.Instances.given
export equality.java_security.Instances.given
export equality.java_sql.Instances.given
export equality.java_text.Instances.given
export equality.java_time.Instances.given
export equality.java_util.Instances.given
10 changes: 10 additions & 0 deletions equality/src/main/scala/equality/core/BuildCheck.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package equality.core

import scala.annotation.implicitNotFound
import scala.util.NotGiven

private val message = "the sources need to be compiled with -language:strictEquality"

private val _ = checkStrictEqualityBuild()

def checkStrictEqualityBuild()(using @implicitNotFound(message) ev: NotGiven[CanEqual[Any, Any]]): Boolean = true
Original file line number Diff line number Diff line change
@@ -1,46 +1,40 @@
package equality
package equality.core

import annotation.implicitNotFound

export Eq.given

// Marker trait for unit tests
private[equality] trait EqTest
export Eq.eqFromAssumed
export Eq.canEqualFromEq

/**
* Strict equality type class with automatic derivation for `Product` types.
* 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 `Product` type
* @tparam T arbitrary type
*/

@implicitNotFound("Values of types ${T} and ${T} cannot be compared with == or !=")
sealed trait Eq[-T]:
/**
* For testing only
*/
/** For testing only. */
private[equality] val violations: Seq[String] = Nil

object Eq:
given eq_from_assumed[A: Eq.assumed]: Eq[A] = Instance
given eq_CanEqual[T: Eq]: CanEqual[T, T] = CanEqual.derived

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
*/
object assumed extends assumed[Any]:
def derived[T]: assumed[T] = assumed
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]]
* @tparam T arbitrary type
*/
sealed trait assumed[-T] extends Eq[T]
object assumed extends assumed[Any]:
def derived[T]: assumed[T] = assumed

/**
* Creates an Eq instance for a `Product` type while verifying equality requirements.
Expand All @@ -51,7 +45,6 @@ object Eq:
*/
inline def derived[T]: Eq[T] = EqMacro.derived


/**
* Identity function that requires an Eq type class instance for the passed argument.
*
Expand All @@ -60,14 +53,22 @@ object Eq:
*/
def apply[T: Eq](value: T): T = value

private object Instance extends Eq[Any]
private object EqAny extends Eq[Any]

private[equality] def apply[T]: Eq[T] = Instance
private[equality] def apply[T]: Eq[T] = EqAny


// For testing only
private[equality] def apply[T](violationSeq: Seq[String]): Eq[T] =
/** For testing only. */
private[equality] def apply[T](testViolations: Seq[String]): Eq[T] =
new Eq[T]:
override val violations = violationSeq

end Eq
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package equality
package equality.core

import scala.quoted.{Expr, Quotes, Type}

Expand All @@ -14,11 +14,11 @@ private[equality] object EqMacro:
val typeRepr = TypeRepr.of[T]
val violations = EqReflection(typeRepr).violations

if typeRepr <:< TypeRepr.of[EqTest] then
if typeRepr <:< TypeRepr.of[ProductTest] then
// Continue the compilation and store the violations in the type class instance for unit test evaluation
'{ Eq[T](${ Expr(violations) }) }
else
violations foreach report.error
'{ Eq[T] }

end EqMacro
end EqMacro
Loading

0 comments on commit 714c562

Please sign in to comment.