Skip to content

Commit

Permalink
11.3.1
Browse files Browse the repository at this point in the history
- 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.
- Restore trait to SameRuntime (requiring by FunctionalInterface tag)

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.
  • Loading branch information
isaacl committed Oct 18, 2024
1 parent 9f308eb commit 8609462
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 75 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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", ""))))
Expand Down
83 changes: 14 additions & 69 deletions core/src/main/scala/newtypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import alleycats.Zero
object newtypes:

@FunctionalInterface
abstract class SameRuntime[A, T]:
trait SameRuntime[A, T]:
// TODO: Convert in both directions...
def apply(a: A): T

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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))
Expand Down
10 changes: 5 additions & 5 deletions model/src/main/scala/model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]

0 comments on commit 8609462

Please sign in to comment.