I have the following code which works well.
package com.andrew
object ExperimentWithTypeConstraints {
def union[T](t: T)(implicit c: (T =:= String)) = t match {
case s: String => println(s"Some nice string: $s")
}
def main(args: Array[String]): Unit = {
union("Hello.")
}
}
Output:
Some nice string: Hello.
I would like to modify the definition of the method union
something like this:
def union[T](t: T)(implicit c: (T =:= String) Or (T =:= Int)) = t match {
case s: String => println(s"Some nice string: $s")
case i: Int => println(s"Some int: $i")
}
The output for union(1)
should be the following one: Some int: 1
Unfortunately, Scala does not know the logical operator Or (And, Not, ..) for such cases and hence it is not possible to compile it. How can I do it?
I found one solution at the following address (http://hacking-scala.org/post/73854628325/advanced-type-constraints-with-type-classes) but unfortunately I don't understand it well. Can you tell me how to solve this problem based on your approach? Or can you explain the solution from the above-mentioned url? Their solution works and it is the following:
@implicitNotFound("Argument does not satisfy constraints: ${A} Or ${B}")
trait Or[A, B]
object Or {
private val evidence: Or[Any, Any] = new Object with Or[Any, Any]
implicit def aExistsEv[A, B](implicit a: A) =
evidence.asInstanceOf[Or[A, B]]
implicit def bExistsEv[A, B](implicit b: B) =
evidence.asInstanceOf[Or[A, B]]
}
I don't understand the part with Or
. Or
is an object. How can I combine this part (T =:= String) Or (T =:= Int)
via Or
together?
Thanks a lot, Andrew