Skip to content

Instantly share code, notes, and snippets.

@colindean
Created September 15, 2014 19:20
Show Gist options
  • Save colindean/ef062db69915b49d44b9 to your computer and use it in GitHub Desktop.
Save colindean/ef062db69915b49d44b9 to your computer and use it in GitHub Desktop.
Trying to improve compareTo to be more idiomatic in Scala pattern matching
// This sucks
3.compareTo(4) match {
case x if x > 0 =>
case x if x < 0 =>
case x if x == 0 =>
}
/*
I want something like this:
3.compareTo(4) match {
case GreaterThan =>
case LessThan =>
case EqualTo =>
}
This would also be acceptable:
3.compareTo(4) match {
case GreaterThan(_) =>
case LessThan(_) =>
case EqualTo(_) =>
}
*/
// But this doesn't seem to work as I'd expect
case class GreaterThan(implicit x: Int) {
def apply(implicit x: Int):Boolean = x > 0
}
case class LessThan(implicit x: Int) {
def apply(implicit x: Int) = x < 0
}
case class EqualTo(implicit x: Int) {
def apply(implicit x: Int) = x == 0
}
3.compareTo(4) match {
case GreaterThan =>
case LessThan =>
case EqualTo =>
}
// I also tried with object apply(). This is something close, but not quite what I want.
object Compare {
def apply(x: Int) = {
x match {
case _ if x > 0 => GreaterThan
case _ if x < 0 => LessThan
case _ if x == 0 => EqualTo
}
}
case class GreaterThan()
case class LessThan()
case class EqualTo()
Compare(3.compareTo(4)) match {
case GreaterThan =>
case LessThan =>
case EqualTo =>
}
// I tried to implement something like this:
Compare(3).to(4) match {
case GreaterThan =>
case LessThan =>
case EqualTo =>
}
// but got caught up on the types:
case class GreaterThan()
case class LessThan()
case class EqualTo()
object Compare {
def apply(x: Ordered) = new Compare(x)
class Compare(original: Ordered) {
def to[T](y: T <: Ordered){ // can't seem to get this right
original.compareTo(y) match { // so that compareTo() gets the correctly typed `y`
case _ if x > 0 => GreaterThan
case _ if x < 0 => LessThan
case _ if x == 0 => EqualTo
}
}}
}
}
@faucct
Copy link

faucct commented Jul 10, 2016

Should not something like this work?

object Greater {
  def unapply(x: Int) = x > 0
}
object Equal {
  def unapply(x: Int) = x == 0
}
object Lesser {
  def unapply(x: Int) = x < 0
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment