Skip to content

Instantly share code, notes, and snippets.

@som-snytt
Created March 23, 2020 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save som-snytt/779e0d75453e5c16e63353ad185e68cc to your computer and use it in GitHub Desktop.
Save som-snytt/779e0d75453e5c16e63353ad185e68cc to your computer and use it in GitHub Desktop.
Shadow implicits
package specsy {
trait Expectable[A]
trait MatchResult[A]
trait Matcher[-T] { outer =>
def apply[S <: T](t: Expectable[S]): MatchResult[S]
}
object Matchers {
def oneMatcher[S1]: Matcher[S1] = OneMatcher.asInstanceOf[Matcher[S1]]
def twoMatcher[S1, S2]: Matcher[(S1, S2)] = TwoMatcher.asInstanceOf[Matcher[(S1, S2)]]
}
object OneMatcher extends Matcher[Any] { def apply[S <: Any](t: Expectable[S]): MatchResult[S] = ??? }
object TwoMatcher extends Matcher[(Any, Any)] { def apply[S <: (Any, Any)](t: Expectable[S]): MatchResult[S] = ??? }
trait Cv { outer =>
def zipf[T1,T2, S1,S2](m1: (=>T1) => Matcher[S1],m2: (=>T2) => Matcher[S2]): (=>(T1,T2)) => Matcher[(S1,S2)] = {
x => Matchers.twoMatcher[S1, S2]
}
implicit class TupleMatcher2[T1,T2](t: (T1,T2)) {
def zipf[S1,S2](m1: (=>T1) => Matcher[S1], m2: (=>T2) => Matcher[S2]): Matcher[(S1,S2)] =
outer.zipf(m1,m2)(t)
}
}
trait Off extends Cv {
implicit class nottm2[T1, T2](val c: Any) {
def zipf[S1,S2](m1: (=>T1) => Matcher[S1], m2: (=>T2) => Matcher[S2]): Matcher[(S1,S2)] = ???
}
/*
implicit class nottm2(val c: Any) {
def zipf[T1,T2,S1,S2](m1: (=>T1) => Matcher[S1], m2: (=>T2) => Matcher[S2]): Matcher[(S1,S2)] = ???
}
*/
//override def TupleMatcher2[T1,T2](t: (T1,T2)) = super.TupleMatcher2(t)
}
object OK extends App with Cv {
val t = (42, 27)
def m(n: => Int): Matcher[Int] = Matchers.oneMatcher[Int]
println(t.zipf(m, m))
}
/*
*/
object DNC extends App with Off {
val t = (42, 27)
def m(n: => Int): Matcher[Int] = Matchers.oneMatcher[Int]
println(t.zipf(m, m))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment