scala - Difficulty writing method with partially applied type parameter and typetag -
i looking way remove type parameter s call apply in following example:
object attribute { trait int [s] extends attribute[s] trait boolean[s] extends attribute[s] // etc. } sealed trait attribute[s] trait attributes[s] { protected def get(key: string): option[attribute[s]] def apply[a <: attribute[s]](key: string) (implicit tag: reflect.classtag[a]): option[a] = get(key) match { case some(attr: a) => some(attr) case _ => none } } with above definition, test case be:
trait test[s] { def map: attributes[s] map[attribute.int[s]]("foo") } what i'm trying do, modify apply definition allow following:
trait test2[s] { def map: attributes[s] map[attribute.int]("foo") // use partially applied attribute type } edit: following on suggestion of marius , comments, why following still producing erasure warning:
import reflect.runtime.universe._ trait attributes[s] { protected def get(key: string): option[attribute[s]] def apply[a[x] <: attribute[x]](key: string) (implicit tag: typetag[a[s]]): option[a[s]] = get(key) match { case some(attr: a[s]) => some(attr) case _ => none } } to me doesn't make sense. on 1 hand have full type tag a[s] available. on other hand, should work in absence, attribute invariant in s, if option[attribute[s]], , match against some(attr: a[x]), possibility x == s.
edit 2: condition solution shape of attribute trait not changed, e.g. not moving type constructor parameter s member field.
have considered leveraging unapply of implicit classtag? if understand docs correctly, unapply tag return none if attr not match type. if match, return of type a[s]. refactored use unapply, code this:
def apply[a[_] <: attribute[_]](key: string) (implicit tag: reflect.classtag[a[s]]): option[a[s]] = get(key) match { case some(attr) => tag.unapply(attr) case _ => none }
Comments
Post a Comment