scala implicit parameter type inference -
running following script under scala 2.10.4. expecting result of a.classop should mypage. why nothing?
scala> trait pagemodel { | def classop[t](implicit manifest: manifest[t]) { | println("class: " + manifest.runtimeclass.getname) | } | } defined trait pagemodel scala> class mypage extends pagemodel defined class mypage scala> val = new mypage a: mypage = mypage@1f2f992 scala> a.classop class: scala.runtime.nothing$ edited:
i think answer. thanks! however, interesting same code running on 2.9.3 gives me java.lang.object. should behave same? see nomanifest in 2.9.3 well.
scala> trait pagemodel{ | def classop[t](implicit m: manifest[t]) { | println("class: " + manifest[t].erasure.getname) | } | } defined trait pagemodel scala> class mypage extends pagemodel defined class mypage scala> val = new mypage a: mypage = mypage@f7bf869 scala> a.classop class: java.lang.object
when define method def classop[t](...) t constrained to? can anything, you're requesting implicit manifest type t can anything: implicit manifest: manifest[t].
if in scala.predef can see following declaration:
val nomanifest = scala.reflect.nomanifest where nomanifest is:
object nomanifest extends optmanifest[nothing] and optmanifest is:
/** `optmanifest[t]` optional [[scala.reflect.manifest]]. * either `manifest` or value `nomanifest`. so translate to?
since nothing subtype of types , there manifest[nothing] in scope (predef in scope) mean implicit injected when nothing else found.
with said, agree ryan , meant do:
trait pagemodel[t] { def classop(implicit manifest: manifest[t]) { println("class: " + manifest.runtimeclass.getname) } }
Comments
Post a Comment