Scala manifest taken from variable and not instance? -


i'm not quite sure how phrase question because don't understand going on. expect manifest able tell me actual runtime type of instance, seems telling me runtime type of variable assigned to.

// scala 2.10.1 trait base class impl1 extends base class impl2 extends base  def showmanifest[t <: base](thing: t)(implicit ev: manifest[t]) = println(thing + ": " + ev.runtimeclass)  val (impl1, impl2) = (new impl1, new impl2)  println("=== impl1 , impl2 ===") showmanifest(impl1) showmanifest(impl2)  val choose1 = if(true) impl1 else impl2 val choose2 = if(false) impl1 else impl2  println("=== choose1 , choose2 ===") showmanifest(choose1) showmanifest(choose2) 

output:

=== impl1 , impl2 === main$$anon$1$impl1@48ff2413: class main$$anon$1$impl1 main$$anon$1$impl2@669980d5: class main$$anon$1$impl2 === choose1 , choose2 === main$$anon$1$impl1@48ff2413: interface main$$anon$1$base main$$anon$1$impl2@669980d5: interface main$$anon$1$base 

so, type of choose1 , choose2 base, why method ends manifest[base]? there way around this, can pick type don't know @ compile time (chosen config parameter or like) , pass factory method?

this seems working expected me. in instances call showmanifest, deferring type inference determine type opposed passing in type explicitly (i.e. showmanifest[impl1](impl1)). type see tied manifest match type of val passing in. in first 2 cases, type inference knows exact concrete type , see reflected in showmanifest prints. in second 2 cases, because of conditionals, type inference not know @ compile time 1 end being , moves type hierarchy 2 types until finds common type ends being base.

i suppose if wanted try around behavior, try this:

import scala.reflect.manifestfactory showmanifest(choose1)(manifestfactory.classtype(choose1.getclass())) 

also, should switch things on typetags/classtags manifest has been deprecated in scala 2.10


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -