I've been experimenting with MacroParadise (here and here), as well a few other newer features. Today while using TypeTags, I came to the realization that I can now do something like this to enforce type equality.
def typeEq[A: TypeTag, B: TypeTag]: Boolean = {
implicitly[TypeTag[A]].tpe =:= implicitly[TypeTag[B]].tpe
}
I then remembered that TypeTag
implicits are compiler generated, and I had the idea that I may be able write a macro enabling more concise TypeTag usage like this:
def foo[A](xs: List[A]): String = xs match {
case y :: ys if typeEq[A, String] => y
case y :: ys if typeEq[A, Int] => y.toString
}
I've only written a handful of macros in Lisp, and am stumbling around attempting to use the macro library. This lead me to several attempts, but they all end up expanding to something like Int =:= Int
which doesn't work, or something like typeA =:= typeB
where both are free(which also doesn't work).
This lead me to two questions:
1) Is this possible to do without the Context Bounds on foo
(like written above)?
2) How do I correctly splice the Type
s obtained by the implicits into the result expression?
It seems to me that macros and implicits should allow me to fetch the WeakTypeTag
implicit and use its tpe
member for splicing, since they both occur at compile time.