Type Erasure in Scala -
i'm rather confused what's happening here:
import scala.collection.immutable._ object main extends app { sealed trait node sealed trait group case class sheet( val splat: string, val charname: string, val children: listmap[string, node], val params0: listmap[string, param], //params0 separate sheet-general parameters val note: option[note] ) extends node group case class attributes(val name: string) extends node group case class param(val name: string, val value: string) extends node case class note(val note: string) extends node i've got 3 versions of replace function - last 1 one i'm trying write, others debugging.
class sheetupdater(s: sheet) { def replace1[t <: group](g: t): unit = { s.children.head match { case (_, _:sheet) => case (_, _:attributes) => } } } this version throws no warnings, apparently have access type of s.children @ runtime.
class sheetupdater(s: sheet) { def replace2[t <: group](g: t): unit = { g match { case _:sheet => case _:attributes => } } } neither version, apparently details of g's type also available @ runtime...
class sheetupdater(s: sheet) { def replace3[t <: group](g: t): unit = { s.children.head match { case (_, _:t) => //! case (_, _:attributes) => } } } ... so, ends throwing me dreaded abstract type pattern t unchecked since eliminated erasure warning. what's going on here?
in scala, generics erased @ runtime, means runtime type of list[int] , list[boolean] same. because jvm whole erases generic types. due because jvm wanted remain backwards compatible way when generics first introduced...
there way around in scala using classtag, implicit parameter can threaded around whatever generic using.
you can think of : classtag passing type of generic argument. (it syntactic sugar passing implicit parameter of type classtag[t].)
import scala.reflect.classtag class sheetupdater(s: sheet) { def replace3[t <: group : classtag](g: t): unit = { s.children.head match { case (_, _:t) => //! case (_, _:attributes) => } } }
Comments
Post a Comment