Skip to content

Instantly share code, notes, and snippets.

@Yotamho
Created November 4, 2020 10:46
Show Gist options
  • Select an option

  • Save Yotamho/86941428651e90d758a5db6f26313dc8 to your computer and use it in GitHub Desktop.

Select an option

Save Yotamho/86941428651e90d758a5db6f26313dc8 to your computer and use it in GitHub Desktop.
object PatternMatchingMorphBehaviour {
// Intention is to morph behaviour of pattern matching,
// in this example I want to make an unapplied value's "equals"
// method always return true if the value it compares against is a None
// (and additionally, if it is a Some, to extract to value out of the Some)
// this way I can do like a "runtime optional matcher"
trait EqualIfNone {
override def equals(obj: Any): Boolean = obj match {
case Some(x) => super.equals(x)
case None => true
case x => super.equals(x)
}
}
object OptionalMatcher {
def unapply[T](arg: T): Option[T] = Some(???) //can't work because I can't override the equals of the object.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment