如何在 Scala 中表达 Vaadin BeanItemContainer 构造函数?

发布于 2024-10-07 15:20:29 字数 1339 浏览 2 评论 0原文

我正在尝试将一堆 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东北女汉子 2024-10-14 15:20:29

您使用 Class.forName 的原因是什么?我认为编译器无法从该调用返回的对象中推断出类型,它只是 Class[_]。如果您使用 classOf,它应该让编译器确定类型:

val bic = new BeanItemContainer[DBObject](classOf[DBObject]))

换句话说: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 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.

甜警司 2024-10-14 15:20:29

试试这个:

val bic: BeanItemContainer[BT] = new BeanItemContainer(Class.forName("com.mongodb.DBObject"))

顺便说一句,您删除了错误所在行的“^”标记。请在粘贴错误消息时保留它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文