I'm trying to load a bunch of com.mongodb.DBObject objects in to a Vaadin BeanItemContainer to display in a table. I'm getting stuck on the translation of the constructor from Java to Scala.
The constructor definition is:
BeanItemContainer(Class<? extends BT> type)
This passes the scala compiler:
val bic = new BeanItemContainer(Class.forName("com.mongodb.DBObject"))
However, when I try to add an item:
mtl.toArray.foreach {t => bic.addBean(t)}
I get the following error:
[ERROR]com/sentientswarm/traderdashboard/UploadTradesWindow.scala:140: error: type mismatch;
found : t.type (with underlying type com.mongodb.DBObject)
required: ?0 where type ?0
mtl.toArray.foreach {t => bic.addBean(t)}
Any thoughts/suggestions?
UPDATE:
Tried:val bic: BeanItemContainer[DBObject] = new BeanItemContainer(Class.forName("com.mongodb.DBObject"))
Result:
[ERROR]com/sentientswarm/traderdashboard/UploadTradesWindow.scala:140: error: type mismatch;
found : java.lang.Class[?0(in value bic)] where 开发者_运维技巧type ?0(in value bic)
required: java.lang.Class[_ <: com.mongodb.DBObject]
val bic: BeanItemContainer[DBObject] = new BeanItemContainer(Class.forName("com.mongodb.DBObject"))
^
Thanks, John
Any reason you're using Class.forName
? I don't think the compiler can infer the type from the returned object from that call, it would just be Class[_]
. If you use classOf
, it should let the compiler determine the type:
val bic = new BeanItemContainer[DBObject](classOf[DBObject]))
In other words: DBObject.class
in Java translates to classOf[DBObject]
in Scala.
Try this:
val bic: BeanItemContainer[BT] = new BeanItemContainer(Class.forName("com.mongodb.DBObject"))
By the way, you removed the "^" marker of where in the line the error is. Please, keep it when pasting error messages.
精彩评论