如何在 Scala 中表达 Vaadin BeanItemContainer 构造函数?
我正在尝试将一堆 com.mongodb.DBObject 对象加载到 Vaadin BeanItemContainer 中以显示在表中。我陷入了将构造函数从 Java 转换为 Scala 的困境。
构造函数定义是:
BeanItemContainer(Class<? extends BT> type)
这通过了 scala 编译器:
val bic = new BeanItemContainer(Class.forName("com.mongodb.DBObject"))
但是,当我尝试添加项目时:
mtl.toArray.foreach {t => bic.addBean(t)}
我收到以下错误:
[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)}
有什么想法/建议吗?
更新:
尝试过:
val bic: BeanItemContainer[DBObject] = new BeanItemContainer(Class.forName("com.mongodb.DBObject"))
结果:
[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"))
^
谢谢, 约翰
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用 Class.forName 的原因是什么?我认为编译器无法从该调用返回的对象中推断出类型,它只是
Class[_]
。如果您使用classOf
,它应该让编译器确定类型:换句话说:Java 中的
DBObject.class
转换为classOf[DBObject]
在斯卡拉。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 beClass[_]
. If you useclassOf
, it should let the compiler determine the type:In other words:
DBObject.class
in Java translates toclassOf[DBObject]
in Scala.试试这个:
顺便说一句,您删除了错误所在行的“^”标记。请在粘贴错误消息时保留它。
Try this:
By the way, you removed the "^" marker of where in the line the error is. Please, keep it when pasting error messages.