scala - Constructing TypeTags of higher-kinded types -
given simple parametrized type class lk[a]
, can write
// or simpler def taglk[a: typetag] = typetag[lk[a]] def taglk[a](implicit ta: typetag[a]) = typetag[lk[a]] taglk[int] == typetag[lk[int]] // true
now i'd write analogue class hk[f[_], a]
:
def taghk[f[_], a](implicit ???) = typetag[hk[f, a]] // or other implementation? taghk[option, int] == typetag[hk[option, int]]
is possible? i've tried
def taghk[f[_], a](implicit tf: typetag[f[_]], ta: typetag[a]) = typetag[hk[f, a]] def taghk[f[_], a](implicit tf: typetag[f], ta: typetag[a]) = typetag[hk[f, a]]
but neither works obvious reasons (in first case f[_]
existential type instead of higher-kinded one, in second typetag[f]
doesn't compile).
i suspect answer "it's impossible", happy if isn't.
edit: use weaktypetag
s follows (slightly simplified):
trait element[a] { val tag: weaktypetag[a] // other irrelevant methods } // e.g. def seqelement[a: element]: element[seq[a]] = new element[seq[a]] { val tag = { implicit val ta = implicitly[element[a]].tag weaktypetag[seq[a]] } } trait container[f[_]] { def lift[a: element]: element[f[a]] // note bound satisfied, pass // tag explicitly when used def tag[a: weaktypetag]: weaktypetag[f[a]] } val seqcontainer: container[seq] = new container[seq] { def lift[a: element] = seqelement[a] }
all of works fine if replace weaktypetag
typetag
. unfortunately, doesn't:
class free[f[_]: container, a: element] def freeelement[f[_]: container, a: element] { val tag = { implicit val ta = implicitly[element[a]].tag // need typetag[f] here // obtained implicit container[f] typetag[free[f, a]] } }
does serve purposes?
def taghk[f[_], a](implicit tt: typetag[hk[f, a]]) = tt
as opposed using implicit parameter typetag
f
, a
separately , composing them, can directly request tag want compiler. passes test case desired:
taghk[option, int] == typetag[hk[option, int]] //true
alternatively, if have instance of typetag[a]
try:
object hk { def apply[f[_]] = new hktypeprovider[f] class hktypeprovider[f[_]] { def get[a](tt: typetag[a])(implicit hktt: typetag[hk[f, a]]) = hktt } }
allowing do:
val mytt = typetag[int] hk[option].get(mytt) == typetag[hk[option, int]] //true
Comments
Post a Comment