From fd0e8ad535492d349a2a22f3f2c427139781af34 Mon Sep 17 00:00:00 2001 From: Isaac Levy Date: Thu, 17 Oct 2024 16:13:14 -0400 Subject: [PATCH] 11.3.1 - Remove extra definitions recently added to OpaqueX - Rename OpaqueInt -> RelaxedOpaqueInt - Rename OpaqueIntSafer -> RichOpaqueInt - Rename FunctionWrapper.apply -> exec The apply method is overloaded with TotalWrapper.apply, leading to spurious build errors. And also it seems like best practice to explicitly unwrap an Opaque function if we want to use it. Note: Currently only use of RichOpaqueInt is Centis. Many users of OpaqueInt in lila need to be renamed to RelaxedOpaqueInt, and many users of FunctionWrapper need the 'exec' method call added. There are related PRs in scalachess, lila-ws, and lila for the upgrade. --- build.sbt | 2 +- core/src/main/scala/newtypes.scala | 81 +++++------------------------- model/src/main/scala/model.scala | 10 ++-- 3 files changed, 19 insertions(+), 74 deletions(-) diff --git a/build.sbt b/build.sbt index 6f9c3f1..87c37ab 100644 --- a/build.sbt +++ b/build.sbt @@ -2,7 +2,7 @@ inThisBuild( Seq( scalaVersion := "3.5.1", versionScheme := Some("early-semver"), - version := "11.3.0", + version := "11.3.1", organization := "org.lichess", licenses += ("MIT" -> url("https://opensource.org/licenses/MIT")), publishTo := Option(Resolver.file("file", new File(sys.props.getOrElse("publishTo", "")))) diff --git a/core/src/main/scala/newtypes.scala b/core/src/main/scala/newtypes.scala index 5c0d83b..b7cdcee 100644 --- a/core/src/main/scala/newtypes.scala +++ b/core/src/main/scala/newtypes.scala @@ -71,22 +71,15 @@ object newtypes: end TotalWrapper abstract class FunctionWrapper[Newtype, Impl](using Newtype =:= Impl) extends TotalWrapper[Newtype, Impl]: - extension (inline a: Newtype) inline def apply: Impl = a.asInstanceOf[Impl] + extension (inline a: Newtype) inline def exec: Impl = a.asInstanceOf[Impl] abstract class OpaqueString[A](using A =:= String) extends TotalWrapper[A, String]: given Show[A] = _.value given Render[A] = _.value - /// --- SIDE NOTE --- - /// Despite looking very similar to each other, the following classes are necessary to split out. Math - /// methods are overloaded and each class uses methods specific to its underlying type. It's possible - /// this could be condensed using @specialized, once it is implemented for scala3 / dotty. - /// ----------------- - - /** Use [[OpaqueIntSafer]] if possible. This class may be removed in the future as it has relaxed type - * safety. + /** [[RichOpaqueInt]] is preferred when possible as it offers more type safety. */ - abstract class OpaqueInt[A](using A =:= Int) extends TotalWrapper[A, Int]: + abstract class RelaxedOpaqueInt[A](using A =:= Int) extends TotalWrapper[A, Int]: extension (inline a: A) inline def unary_- : A = apply(-raw(a)) inline infix def >(inline o: Int): Boolean = raw(a) > o @@ -105,35 +98,9 @@ object newtypes: inline infix def -(inline o: A): A = a - raw(o) inline def atLeast(inline bot: A): A = atLeast(raw(bot)) inline def atMost(inline top: A): A = atMost(raw(top)) - end OpaqueInt - - abstract class OpaqueIntSafer[A](using A =:= Int) extends TotalWrapper[A, Int]: - extension (inline a: A) - inline def unary_- : A = apply(-raw(a)) - inline infix def >(inline o: A): Boolean = raw(a) > raw(o) - inline infix def <(inline o: A): Boolean = raw(a) < raw(o) - inline infix def >=(inline o: A): Boolean = raw(a) >= raw(o) - inline infix def <=(inline o: A): Boolean = raw(a) <= raw(o) - inline infix def +(inline o: A): A = apply(raw(a) + raw(o)) - inline infix def -(inline o: A): A = apply(raw(a) - raw(o)) - inline def atLeast(inline bot: A): A = apply(Math.max(raw(a), raw(bot))) - inline def atMost(inline top: A): A = apply(Math.min(raw(a), raw(top))) - end OpaqueIntSafer - - abstract class OpaqueLong[A](using A =:= Long) extends TotalWrapper[A, Long]: - extension (inline a: A) - inline def unary_- : A = apply(-raw(a)) - inline infix def >(inline o: A): Boolean = raw(a) > raw(o) - inline infix def <(inline o: A): Boolean = raw(a) < raw(o) - inline infix def >=(inline o: A): Boolean = raw(a) >= raw(o) - inline infix def <=(inline o: A): Boolean = raw(a) <= raw(o) - inline infix def +(inline o: A): A = apply(raw(a) + raw(o)) - inline infix def -(inline o: A): A = apply(raw(a) - raw(o)) - inline def atLeast(inline bot: A): A = apply(Math.max(raw(a), raw(bot))) - inline def atMost(inline top: A): A = apply(Math.min(raw(a), raw(top))) - end OpaqueLong + end RelaxedOpaqueInt - abstract class OpaqueDouble[A](using A =:= Double) extends TotalWrapper[A, Double]: + abstract class RichOpaqueInt[A](using A =:= Int) extends TotalWrapper[A, Int]: extension (inline a: A) inline def unary_- : A = apply(-raw(a)) inline infix def >(inline o: A): Boolean = raw(a) > raw(o) @@ -144,41 +111,19 @@ object newtypes: inline infix def -(inline o: A): A = apply(raw(a) - raw(o)) inline def atLeast(inline bot: A): A = apply(Math.max(raw(a), raw(bot))) inline def atMost(inline top: A): A = apply(Math.min(raw(a), raw(top))) + end RichOpaqueInt - @deprecated("Unsafe and be removed later.", "11.3.0") - inline def +(inline o: Int): A = apply(raw(a) + o) - end OpaqueDouble - - abstract class OpaqueFloat[A](using A =:= Float) extends TotalWrapper[A, Float]: - extension (inline a: A) - inline def unary_- : A = apply(-raw(a)) - inline infix def >(inline o: A): Boolean = raw(a) > raw(o) - inline infix def <(inline o: A): Boolean = raw(a) < raw(o) - inline infix def >=(inline o: A): Boolean = raw(a) >= raw(o) - inline infix def <=(inline o: A): Boolean = raw(a) <= raw(o) - inline infix def +(inline o: A): A = apply(raw(a) + raw(o)) - inline infix def -(inline o: A): A = apply(raw(a) - raw(o)) - inline def atLeast(inline bot: A): A = apply(Math.max(raw(a), raw(bot))) - inline def atMost(inline top: A): A = apply(Math.min(raw(a), raw(top))) - end OpaqueFloat + abstract class OpaqueInt[A](using A =:= Int) extends TotalWrapper[A, Int] + abstract class OpaqueLong[A](using A =:= Long) extends TotalWrapper[A, Long] + abstract class OpaqueDouble[A](using A =:= Double) extends TotalWrapper[A, Double] + abstract class OpaqueFloat[A](using A =:= Float) extends TotalWrapper[A, Float] import scala.concurrent.duration.FiniteDuration - abstract class OpaqueDuration[A](using A =:= FiniteDuration) extends TotalWrapper[A, FiniteDuration]: - extension (inline a: A) - inline def unary_- : A = apply(-raw(a)) - inline infix def >(inline o: A): Boolean = raw(a) > raw(o) - inline infix def <(inline o: A): Boolean = raw(a) < raw(o) - inline infix def >=(inline o: A): Boolean = raw(a) >= raw(o) - inline infix def <=(inline o: A): Boolean = raw(a) <= raw(o) - inline infix def +(inline o: A): A = apply(raw(a) + raw(o)) - inline infix def -(inline o: A): A = apply(raw(a) - raw(o)) - inline def atLeast(inline bot: A): A = apply(raw(a).max(raw(bot))) - inline def atMost(inline top: A): A = apply(raw(a).min(raw(top))) - end OpaqueDuration + abstract class OpaqueDuration[A](using A =:= FiniteDuration) extends TotalWrapper[A, FiniteDuration] abstract class YesNo[A](using A =:= Boolean) extends TotalWrapper[A, Boolean]: - final val Yes: A = apply(true) - final val No: A = apply(false) + final def Yes: A = apply(true) + final def No: A = apply(false) extension (inline a: A) inline def flip: A = apply(!raw(a)) diff --git a/model/src/main/scala/model.scala b/model/src/main/scala/model.scala index a4713d7..80f56f1 100644 --- a/model/src/main/scala/model.scala +++ b/model/src/main/scala/model.scala @@ -5,16 +5,16 @@ import scalalib.newtypes.* object model: opaque type Max = Int - object Max extends OpaqueInt[Max] + object Max extends RelaxedOpaqueInt[Max] opaque type MaxPerPage = Int - object MaxPerPage extends OpaqueInt[MaxPerPage] + object MaxPerPage extends RelaxedOpaqueInt[MaxPerPage] opaque type MaxPerSecond = Int - object MaxPerSecond extends OpaqueInt[MaxPerSecond] + object MaxPerSecond extends RelaxedOpaqueInt[MaxPerSecond] opaque type Days = Int - object Days extends OpaqueInt[Days] + object Days extends RelaxedOpaqueInt[Days] opaque type Seconds = Int - object Seconds extends OpaqueInt[Seconds] + object Seconds extends RelaxedOpaqueInt[Seconds]