Skip to content

Commit

Permalink
Move equality singleton ops to scala.compiletime.ops.any
Browse files Browse the repository at this point in the history
This is more consistent with the other ops, which are also split 
depending on the type of the argument.
  • Loading branch information
MaximeKjaer committed Dec 29, 2019
1 parent 2cab591 commit 8c117c1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
5 changes: 3 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ class Definitions {
@tu lazy val CompiletimeTesting_ErrorKind_Parser: Symbol = CompiletimeTesting_ErrorKind.requiredMethod("Parser")
@tu lazy val CompiletimeTesting_ErrorKind_Typer: Symbol = CompiletimeTesting_ErrorKind.requiredMethod("Typer")
@tu lazy val CompiletimeOpsPackageObject: Symbol = ctx.requiredModule("scala.compiletime.ops.package")
@tu lazy val CompiletimeOpsPackageObjectAny: Symbol = ctx.requiredModule("scala.compiletime.ops.package.any")
@tu lazy val CompiletimeOpsPackageObjectInt: Symbol = ctx.requiredModule("scala.compiletime.ops.package.int")
@tu lazy val CompiletimeOpsPackageObjectString: Symbol = ctx.requiredModule("scala.compiletime.ops.package.string")
@tu lazy val CompiletimeOpsPackageObjectBoolean: Symbol = ctx.requiredModule("scala.compiletime.ops.package.boolean")
Expand Down Expand Up @@ -902,7 +903,7 @@ class Definitions {
final def isCompiletime_S(sym: Symbol)(implicit ctx: Context): Boolean =
sym.name == tpnme.S && sym.owner == CompiletimePackageObject.moduleClass

private val compiletimePackageTypes: Set[Name] = Set(tpnme.Equals, tpnme.NotEquals)
private val compiletimePackageAnyTypes: Set[Name] = Set(tpnme.Equals, tpnme.NotEquals)
private val compiletimePackageIntTypes: Set[Name] = Set(
tpnme.Plus, tpnme.Minus, tpnme.Times, tpnme.Div, tpnme.Mod,
tpnme.Lt, tpnme.Gt, tpnme.Ge, tpnme.Le,
Expand All @@ -913,7 +914,7 @@ class Definitions {

final def isCompiletimeAppliedType(sym: Symbol)(implicit ctx: Context): Boolean = {
def isOpsPackageObjectAppliedType: Boolean =
sym.owner == CompiletimeOpsPackageObject.moduleClass && compiletimePackageTypes.contains(sym.name) ||
sym.owner == CompiletimeOpsPackageObjectAny.moduleClass && compiletimePackageAnyTypes.contains(sym.name) ||
sym.owner == CompiletimeOpsPackageObjectInt.moduleClass && compiletimePackageIntTypes.contains(sym.name) ||
sym.owner == CompiletimeOpsPackageObjectBoolean.moduleClass && compiletimePackageBooleanTypes.contains(sym.name) ||
sym.owner == CompiletimeOpsPackageObjectString.moduleClass && compiletimePackageStringTypes.contains(sym.name)
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3643,7 +3643,7 @@ object Types {
if (owner == defn.CompiletimePackageObject.moduleClass) name match {
case tpnme.S if nArgs == 1 => constantFold1(natValue, _ + 1)
case _ => None
} else if (owner == defn.CompiletimeOpsPackageObject.moduleClass) name match {
} else if (owner == defn.CompiletimeOpsPackageObjectAny.moduleClass) name match {
case tpnme.Equals if nArgs == 2 => constantFold2(constValue, _ == _)
case tpnme.NotEquals if nArgs == 2 => constantFold2(constValue, _ != _)
case _ => None
Expand Down
36 changes: 19 additions & 17 deletions library/src/scala/compiletime/ops/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@ package scala.compiletime
import scala.annotation.infix

package object ops {
/** Equality comparison of two singleton types.
* ```scala
* val eq1: 1 == 1 = true
* val eq2: 1 == "1" = false
* val eq3: "1" == "1" = true
* ```
*/
@infix type ==[X <: AnyVal, Y <: AnyVal] <: Boolean

/** Inequality comparison of two singleton types.
* ```scala
* val eq1: 1 != 1 = false
* val eq2: 1 != "1" = true
* val eq3: "1" != "1" = false
* ```
*/
@infix type !=[X <: AnyVal, Y <: AnyVal] <: Boolean
object any {
/** Equality comparison of two singleton types.
* ```scala
* val eq1: 1 == 1 = true
* val eq2: 1 == "1" = false
* val eq3: "1" == "1" = true
* ```
*/
@infix type ==[X <: AnyVal, Y <: AnyVal] <: Boolean

/** Inequality comparison of two singleton types.
* ```scala
* val eq1: 1 != 1 = false
* val eq2: 1 != "1" = true
* val eq3: "1" != "1" = false
* ```
*/
@infix type !=[X <: AnyVal, Y <: AnyVal] <: Boolean
}

object string {
/** Concatenation of two `String` singleton types.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import scala.compiletime.ops._
import scala.compiletime.ops.any._

object Test {
val t32: 1 == 1 = true
Expand Down

0 comments on commit 8c117c1

Please sign in to comment.