generics - Very weird type mismatch in Scala -


why error when firing printgenerictype(new box[master]()) , , not error in case if run printgenerictype(new box()) on new box[master]()?

object varianceexample extends app {    new box[master]().printgenerictype(new box()) // ok   new box().printgenerictype(new box[master]()) // fail  }  class box[t >: tool](implicit m: manifest[t]) {   def printgenerictype(box: box[t]) = {     println(s"generic type [$m] - $box")   } }  class master class tool   extends master class hammer extends tool 

you need define type t covariant, otherwise can pass same type used in constructor. new box()- gives box[tool] (without other constraints) , trying pass box[master].

in first example scalac automatically infers new box() box[master] because it's in box[master] position.

not sure want achieve, solve concrete problem need define type box this:

class box[+t >: tool](implicit m: manifest[t]) {   def printgenerictype[a >: t](box: box[a]) = {     println(s"generic type [$m] - $box")   } } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -