Skip to content

Commit

Permalink
Make erased instances public and sealed
Browse files Browse the repository at this point in the history
Drop final from methods of final classes.
  • Loading branch information
joroKr21 committed Feb 18, 2024
1 parent 7b49621 commit c99e22c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 71 deletions.
13 changes: 1 addition & 12 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import com.typesafe.tools.mima.core.{ProblemFilters, ReversedMissingMethodProblem}

val scala3Version = "3.3.1"

ThisBuild / organization := "org.typelevel"
Expand Down Expand Up @@ -64,16 +62,7 @@ lazy val deriving = crossProject(JSPlatform, JVMPlatform, NativePlatform)
.settings(commonSettings)
.jsEnablePlugins(ScalaJSJUnitPlugin)
.nativeEnablePlugins(ScalaNativeJUnitPlugin)
.settings(
libraryDependencies += "org.typelevel" %%% "cats-core" % "2.10.0" % "test",
mimaBinaryIssueFilters ++= Seq(
ProblemFilters.exclude[ReversedMissingMethodProblem]("shapeless3.deriving.internals.ErasedInstances.erasedMapK"),
ProblemFilters
.exclude[ReversedMissingMethodProblem]("shapeless3.deriving.internals.ErasedProductInstances.erasedProject"),
ProblemFilters
.exclude[ReversedMissingMethodProblem]("shapeless3.deriving.internals.ErasedProductInstances.erasedMapK")
)
)
.settings(libraryDependencies += "org.typelevel" %%% "cats-core" % "2.10.0" % "test")

lazy val derivingJVM = deriving.jvm
lazy val derivingJS = deriving.js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import scala.deriving.*

import shapeless3.deriving.*

abstract private[shapeless3] class ErasedInstances[K, FT] extends Serializable:
sealed abstract class ErasedInstances[K, FT] extends Serializable:
def erasedMapK(f: Any => Any): ErasedInstances[K, ?]
def erasedMap(x: Any)(f: (Any, Any) => Any): Any
def erasedTraverse[F[_]](x: Any)(map: MapF[F])(pure: Pure[F])(ap: Ap[F])(f: (Any, Any) => F[Any]): F[Any]

abstract private[shapeless3] class ErasedProductInstances[K, FT] extends ErasedInstances[K, FT]:
sealed abstract class ErasedProductInstances[K, FT] extends ErasedInstances[K, FT]:
def erasedMapK(f: Any => Any): ErasedProductInstances[K, ?]
def erasedConstruct(f: Any => Any): Any
def erasedConstructA[F[_]](f: Any => F[Any])(pure: Pure[F], map: MapF[F], ap: Ap[F]): F[Any]
Expand All @@ -41,86 +41,81 @@ abstract private[shapeless3] class ErasedProductInstances[K, FT] extends ErasedI
def erasedFoldRight2(x0: Any, y0: Any)(a: Any)(f: (Any, Any, Any, Any) => CompleteOr[Any]): Any
def erasedProject(x0: Any)(p: Int)(f: (Any, Any) => Any): Any

final private[shapeless3] class ErasedProductInstances1[K, FT](val mirror: Mirror.Product, i0: () => Any)
extends ErasedProductInstances[K, FT]:
final class ErasedProductInstances1[K, FT](mirror: Mirror.Product, i0: () => Any) extends ErasedProductInstances[K, FT]:

@deprecated(
"Preserved for bincompat reasons. DO NOT USE as it will lead to stack overflows when deriving instances for recursive types"
)
@deprecated("""Preserved for binary compatibility reasons.
DO NOT USE as it will lead to stack overflows when deriving instances for recursive types.""")
def this(mirror: Mirror.Product, i0: Any) = this(mirror, () => i0)

lazy val i = i0()

inline def toProduct(x: Any): Product = x.asInstanceOf[Product]
private lazy val i: Any = i0()
private inline def toProduct(x: Any): Product = x.asInstanceOf[Product]
private def toElement(x: Any) = toProduct(x).productElement(0)
private def fromElement(x: Any) = mirror.fromProduct(Tuple1(x))

final def erasedMapK(f: Any => Any): ErasedProductInstances[K, ?] =
def erasedMapK(f: Any => Any): ErasedProductInstances[K, ?] =
new ErasedProductInstances1(mirror, () => f(i))

final def erasedConstruct(f: Any => Any): Any =
def erasedConstruct(f: Any => Any): Any =
fromElement(f(i))

final def erasedConstructA[F[_]](f: Any => F[Any])(pure: Pure[F], map: MapF[F], ap: Ap[F]): F[Any] =
def erasedConstructA[F[_]](f: Any => F[Any])(pure: Pure[F], map: MapF[F], ap: Ap[F]): F[Any] =
map(f(i), fromElement)

final def erasedConstructM[F[_]](f: Any => F[Any])(pure: Pure[F], map: MapF[F], tailRecM: TailRecM[F]): F[Any] =
def erasedConstructM[F[_]](f: Any => F[Any])(pure: Pure[F], map: MapF[F], tailRecM: TailRecM[F]): F[Any] =
map(f(i), fromElement)

final def erasedUnfold(a: Any)(f: (Any, Any) => (Any, Option[Any])): (Any, Option[Any]) =
def erasedUnfold(a: Any)(f: (Any, Any) => (Any, Option[Any])): (Any, Option[Any]) =
val (acc0, e0) = f(a, i)
e0 match
case Some(_) => (acc0, Some(mirror.fromProduct(e0)))
case None => (acc0, None)

final def erasedMap(x: Any)(f: (Any, Any) => Any): Any =
def erasedMap(x: Any)(f: (Any, Any) => Any): Any =
fromElement(f(i, toElement(x)))

final def erasedTraverse[F[_]](x: Any)(map: MapF[F])(pure: Pure[F])(ap: Ap[F])(f: (Any, Any) => F[Any]): F[Any] =
def erasedTraverse[F[_]](x: Any)(map: MapF[F])(pure: Pure[F])(ap: Ap[F])(f: (Any, Any) => F[Any]): F[Any] =
map(f(i, toElement(x)), fromElement)

final def erasedMap2(x: Any, y: Any)(f: (Any, Any, Any) => Any): Any =
def erasedMap2(x: Any, y: Any)(f: (Any, Any, Any) => Any): Any =
fromElement(f(i, toElement(x), toElement(y)))

final def erasedFoldLeft(x: Any)(a: Any)(f: (Any, Any, Any) => CompleteOr[Any]): Any =
def erasedFoldLeft(x: Any)(a: Any)(f: (Any, Any, Any) => CompleteOr[Any]): Any =
f(a, i, toElement(x)) match
case Complete(r) => r
case acc => acc

final def erasedFoldLeft2(x: Any, y: Any)(a: Any)(f: (Any, Any, Any, Any) => CompleteOr[Any]): Any =
def erasedFoldLeft2(x: Any, y: Any)(a: Any)(f: (Any, Any, Any, Any) => CompleteOr[Any]): Any =
f(a, i, toElement(x), toElement(y)) match
case Complete(r) => r
case acc => acc

final def erasedFoldRight(x: Any)(a: Any)(f: (Any, Any, Any) => CompleteOr[Any]): Any =
def erasedFoldRight(x: Any)(a: Any)(f: (Any, Any, Any) => CompleteOr[Any]): Any =
f(i, toElement(x), a) match
case Complete(r) => r
case acc => acc

final def erasedFoldRight2(x: Any, y: Any)(a: Any)(f: (Any, Any, Any, Any) => CompleteOr[Any]): Any =
def erasedFoldRight2(x: Any, y: Any)(a: Any)(f: (Any, Any, Any, Any) => CompleteOr[Any]): Any =
f(i, toElement(x), toElement(y), a) match
case Complete(r) => r
case acc => acc

final def erasedProject(x: Any)(p: Int)(f: (Any, Any) => Any): Any =
def erasedProject(x: Any)(p: Int)(f: (Any, Any) => Any): Any =
f(i, toElement(x))

object ErasedProductInstances1:
def apply[K, FT](mirror: Mirror.Product, i: => Any): ErasedProductInstances1[K, FT] =
new ErasedProductInstances1(mirror, () => i)

final private[shapeless3] class ErasedProductInstancesN[K, FT](val mirror: Mirror.Product, is0: () => Array[Any])
final class ErasedProductInstancesN[K, FT](mirror: Mirror.Product, is0: () => Array[Any])
extends ErasedProductInstances[K, FT]:
import ErasedProductInstances.*

@deprecated(
"Preserved for bincompat reasons. DO NOT USE as it will lead to stack overflows when deriving instances for recursive types"
)
@deprecated("""Preserved for binary compatibility reasons.
DO NOT USE as it will lead to stack overflows when deriving instances for recursive types.""")
def this(mirror: Mirror.Product, is0: Array[Any]) = this(mirror, () => is0)

lazy val is: Array[Any] = is0()

inline def toProduct(x: Any): Product = x.asInstanceOf[Product]
private lazy val is: Array[Any] = is0()
private inline def toProduct(x: Any): Product = x.asInstanceOf[Product]
private def fromArray(xs: Array[Any]) = mirror.fromProduct(new ArrayProduct(xs))
private def fromIndexedSeq(xs: IndexedSeq[Any]) = mirror.fromProduct(new IndexedSeqProduct(xs))
private def fromEmptyProduct = mirror.fromProduct(None)
Expand All @@ -140,10 +135,10 @@ final private[shapeless3] class ErasedProductInstancesN[K, FT](val mirror: Mirro
map(acc, fromIndexedSeq)
end traverseProduct

final def erasedMapK(f: Any => Any): ErasedProductInstances[K, ?] =
def erasedMapK(f: Any => Any): ErasedProductInstances[K, ?] =
new ErasedProductInstancesN(mirror, () => is.map(f))

final def erasedConstruct(f: Any => Any): Any =
def erasedConstruct(f: Any => Any): Any =
val n = is.length
if n == 0 then fromEmptyProduct
else
Expand All @@ -154,10 +149,10 @@ final private[shapeless3] class ErasedProductInstancesN[K, FT](val mirror: Mirro
i = i + 1
fromArray(arr)

final def erasedConstructA[F[_]](f: Any => F[Any])(pure: Pure[F], map: MapF[F], ap: Ap[F]): F[Any] =
def erasedConstructA[F[_]](f: Any => F[Any])(pure: Pure[F], map: MapF[F], ap: Ap[F]): F[Any] =
traverseProduct(new ArrayProduct(is), (tc, _) => f(tc))(pure, map, ap)

final def erasedConstructM[F[_]](f: Any => F[Any])(pure: Pure[F], map: MapF[F], tailRecM: TailRecM[F]): F[Any] =
def erasedConstructM[F[_]](f: Any => F[Any])(pure: Pure[F], map: MapF[F], tailRecM: TailRecM[F]): F[Any] =
val n = is.length
def step(xs: Vector[Any]) =
val i = xs.length
Expand All @@ -167,7 +162,7 @@ final private[shapeless3] class ErasedProductInstancesN[K, FT](val mirror: Mirro
else tailRecM(Vector.empty, step)
end erasedConstructM

final def erasedUnfold(a: Any)(f: (Any, Any) => (Any, Option[Any])): (Any, Option[Any]) =
def erasedUnfold(a: Any)(f: (Any, Any) => (Any, Option[Any])): (Any, Option[Any]) =
val n = is.length
if n == 0 then (a, Some(fromEmptyProduct))
else
Expand All @@ -185,7 +180,7 @@ final private[shapeless3] class ErasedProductInstancesN[K, FT](val mirror: Mirro
i = i + 1
(acc, Some(fromArray(arr)))

final def erasedMap(x0: Any)(f: (Any, Any) => Any): Any =
def erasedMap(x0: Any)(f: (Any, Any) => Any): Any =
val n = is.length
if n == 0 then x0
else
Expand All @@ -197,10 +192,10 @@ final private[shapeless3] class ErasedProductInstancesN[K, FT](val mirror: Mirro
i = i + 1
fromArray(arr)

final def erasedTraverse[F[_]](x: Any)(map: MapF[F])(pure: Pure[F])(ap: Ap[F])(f: (Any, Any) => F[Any]): F[Any] =
def erasedTraverse[F[_]](x: Any)(map: MapF[F])(pure: Pure[F])(ap: Ap[F])(f: (Any, Any) => F[Any]): F[Any] =
traverseProduct(toProduct(x), f)(pure, map, ap)

final def erasedMap2(x0: Any, y0: Any)(f: (Any, Any, Any) => Any): Any =
def erasedMap2(x0: Any, y0: Any)(f: (Any, Any, Any) => Any): Any =
val n = is.length
if n == 0 then x0
else
Expand All @@ -213,7 +208,7 @@ final private[shapeless3] class ErasedProductInstancesN[K, FT](val mirror: Mirro
i = i + 1
fromArray(arr)

final def erasedFoldLeft(x0: Any)(i: Any)(f: (Any, Any, Any) => CompleteOr[Any]): Any =
def erasedFoldLeft(x0: Any)(i: Any)(f: (Any, Any, Any) => CompleteOr[Any]): Any =
val n = is.length
if n == 0 then i
else
Expand All @@ -229,7 +224,7 @@ final private[shapeless3] class ErasedProductInstancesN[K, FT](val mirror: Mirro

loop(0, i)

final def erasedFoldRight(x0: Any)(i: Any)(f: (Any, Any, Any) => CompleteOr[Any]): Any =
def erasedFoldRight(x0: Any)(i: Any)(f: (Any, Any, Any) => CompleteOr[Any]): Any =
val n = is.length
if n == 0 then i
else
Expand All @@ -245,7 +240,7 @@ final private[shapeless3] class ErasedProductInstancesN[K, FT](val mirror: Mirro

loop(n - 1, i)

final def erasedFoldLeft2(x0: Any, y0: Any)(i: Any)(f: (Any, Any, Any, Any) => CompleteOr[Any]): Any =
def erasedFoldLeft2(x0: Any, y0: Any)(i: Any)(f: (Any, Any, Any, Any) => CompleteOr[Any]): Any =
val n = is.length
if n == 0 then i
else
Expand All @@ -262,7 +257,7 @@ final private[shapeless3] class ErasedProductInstancesN[K, FT](val mirror: Mirro

loop(0, i)

final def erasedFoldRight2(x0: Any, y0: Any)(i: Any)(f: (Any, Any, Any, Any) => CompleteOr[Any]): Any =
def erasedFoldRight2(x0: Any, y0: Any)(i: Any)(f: (Any, Any, Any, Any) => CompleteOr[Any]): Any =
val n = is.length
if n == 0 then i
else
Expand All @@ -279,14 +274,14 @@ final private[shapeless3] class ErasedProductInstancesN[K, FT](val mirror: Mirro

loop(n - 1, i)

final def erasedProject(x0: Any)(p: Int)(f: (Any, Any) => Any): Any =
def erasedProject(x0: Any)(p: Int)(f: (Any, Any) => Any): Any =
f(is(p), toProduct(x0).productElement(p))

object ErasedProductInstancesN:
def apply[K, FT](mirror: Mirror.Product, is: => Array[Any]): ErasedProductInstancesN[K, FT] =
new ErasedProductInstancesN(mirror, () => is)

private[shapeless3] object ErasedProductInstances:
object ErasedProductInstances:
class ArrayProduct(val elems: Array[Any]) extends Product:
def canEqual(that: Any): Boolean = true
def productElement(n: Int): Any = elems(n)
Expand All @@ -299,55 +294,55 @@ private[shapeless3] object ErasedProductInstances:
def productArity: Int = elems.length
override def productIterator: Iterator[Any] = elems.iterator

inline def summonOne[T] = inline erasedValue[T] match
private inline def summonOne[T] = inline erasedValue[T] match
case _: Tuple1[a] => summonInline[a]

val emptyArray: Array[Any] = new Array(0)
private val emptyArray: Array[Any] = new Array(0)

inline def apply[K, FT, E <: Tuple](mirror: Mirror.Product): ErasedProductInstances[K, FT] =
inline erasedValue[Tuple.Size[E]] match
case 0 => ErasedProductInstancesN[K, FT](mirror, emptyArray)
case 1 => ErasedProductInstances1[K, FT](mirror, summonOne[E])
case _ => ErasedProductInstancesN[K, FT](mirror, summonAsArray[E])

final private[shapeless3] class ErasedCoproductInstances[K, FT](mirror: Mirror.Sum, is0: => Array[Any])
extends ErasedInstances[K, FT]:
lazy val is = is0
final class ErasedCoproductInstances[K, FT](mirror: Mirror.Sum, is0: => Array[Any]) extends ErasedInstances[K, FT]:
private lazy val is: Array[Any] = is0

final def erasedMapK(f: Any => Any): ErasedCoproductInstances[K, ?] =
def erasedMapK(f: Any => Any): ErasedCoproductInstances[K, ?] =
new ErasedCoproductInstances(mirror, is.map(f))

final def ordinal(x: Any): Any = is(mirror.ordinal(x.asInstanceOf))
private def ordinal(x: Any): Any =
is(mirror.ordinal(x.asInstanceOf))

final def erasedMap(x: Any)(f: (Any, Any) => Any): Any =
def erasedMap(x: Any)(f: (Any, Any) => Any): Any =
val i = ordinal(x)
f(i, x)

final def erasedProject(p: Int)(i: Any)(f: (Any, Any) => (Any, Option[Any])): (Any, Option[Any]) =
def erasedProject(p: Int)(i: Any)(f: (Any, Any) => (Any, Option[Any])): (Any, Option[Any]) =
f(i, is(p))

final def erasedInject(p: Int)(f: Any => Any): Any =
def erasedInject(p: Int)(f: Any => Any): Any =
f(is(p))

final def erasedFold(x: Any)(f: (Any, Any) => Any): Any =
def erasedFold(x: Any)(f: (Any, Any) => Any): Any =
val i = ordinal(x)
f(i, x)

final def erasedTraverse[F[_]](x: Any)(map: MapF[F])(pure: Pure[F])(ap: Ap[F])(f: (Any, Any) => F[Any]): F[Any] =
def erasedTraverse[F[_]](x: Any)(map: MapF[F])(pure: Pure[F])(ap: Ap[F])(f: (Any, Any) => F[Any]): F[Any] =
f(ordinal(x), x)

final def erasedFold2(x: Any, y: Any)(a: => Any)(f: (Any, Any, Any) => Any): Any =
def erasedFold2(x: Any, y: Any)(a: => Any)(f: (Any, Any, Any) => Any): Any =
val i = mirror.ordinal(x.asInstanceOf)
val j = mirror.ordinal(y.asInstanceOf)
if i == j then f(is(i), x, y)
else a

final def erasedFold2f(x: Any, y: Any)(g: (Int, Int) => Any)(f: (Any, Any, Any) => Any): Any =
def erasedFold2f(x: Any, y: Any)(g: (Int, Int) => Any)(f: (Any, Any, Any) => Any): Any =
val i = mirror.ordinal(x.asInstanceOf)
val j = mirror.ordinal(y.asInstanceOf)
if i == j then f(is(i), x, y)
else g(i, j)

private[shapeless3] object ErasedCoproductInstances:
object ErasedCoproductInstances:
inline def apply[K, FT, E <: Tuple](mirror: Mirror.Sum): ErasedCoproductInstances[K, FT] =
new ErasedCoproductInstances[K, FT](mirror, summonAsArray[E])

0 comments on commit c99e22c

Please sign in to comment.