scala - Equivalence of two types without consideing type parameters -
scala - Equivalence of two types without consideing type parameters -
i want implicit evidence appears follows:
def foo[a, b](implicit ev: ???[a, b]) = ev foo[int, int] //○ (compiles) foo[any, int] //× (fails) foo[seq[int], seq[int]] //○ foo[seq[any], seq[int]] //○ foo[seq[int], list[int]] //×
so 2 types must of same class, type parameters don't matter (just classtag
.) how implement such function? utilize macro?
thank you!
my friend answered outside xd
sealed trait ~[a, b] object ~ { implicit def a[a, b](implicit ev: =:= b) = new ~[a, b] {} implicit def fa[f[_], a, b] = new ~[f[a], f[b]] {} }
run:
scala> implicitly[int ~ int] res0: ~[int,int] = $tilde$$anon$1@574ae207 scala> implicitly[int ~ any] <console>:10: error: not find implicit value parameter e: ~[int,any] implicitly[int ~ any] ^ scala> implicitly[seq[int] ~ seq[any]] res2: ~[seq[int],seq[any]] = $tilde$$anon$2@27dc1857 scala> implicitly[seq[int] ~ seq[int]] res3: ~[seq[int],seq[int]] = $tilde$$anon$2@69c33436 scala> implicitly[seq[int] ~ list[int]] <console>:10: error: not find implicit value parameter e: ~[seq[int],list[int]] implicitly[seq[int] ~ list[int]] ^
scala macros types implicit
Comments
Post a Comment