闭包意味着完全类型安全的标准?

发布于 2024-08-12 11:00:11 字数 606 浏览 10 评论 0原文

结合闭包(FCM)和泛型,是否有可能拥有完全类型安全的标准。

// The following works without a cast as Foo.id is a 'long' field.
List<Long> ids = session.createCriteria(Foo.class)
                        .setProjection(Foo#id)
                        .list();

// The following is a compilation error, as Foo.bar is defined as an int, and not a string
session.createCriteria(Foo.class)
       .addRestriction(Restrictions.eq(Foo#bar,"blah"))
       .list();

我已阅读 JPA 2.0 规范以了解类型安全标准。但还是有些欠缺。

此外,我只是在这里使用标准作为总体上改进代码类型安全性的示例。我大量使用 java 的静态类型来让我编码得更快。但结果是我时不时地被代码中忽略输入的部分所困扰。例如 HQL 查询。

combining closures (FCM) and generics, would it be possible to have fully type-safe criteria.

// The following works without a cast as Foo.id is a 'long' field.
List<Long> ids = session.createCriteria(Foo.class)
                        .setProjection(Foo#id)
                        .list();

// The following is a compilation error, as Foo.bar is defined as an int, and not a string
session.createCriteria(Foo.class)
       .addRestriction(Restrictions.eq(Foo#bar,"blah"))
       .list();

I've read the JPA 2.0 spec for type-safe criteria. But its still somewhat lacking.

Besides, I'm just using criteria here as an example of improving the type-safety of code in general. I used the static typing of java heavily to let me code faster. But as a result I get bitten now and then by the parts of my code that ignore typing. For example HQL queries.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

少钕鈤記 2024-08-19 11:00:11

您描述的代码不使用闭包,而是使用字段文字方法文字)。就像古老的类文字 。这些可以帮助制定标准 API。类型安全查询接口的 JPA 2 源代码生成解决方法 可以用它替换。 如果它将成为 JDK7 的一部分。

The code you're describing does not use closures but field literals (method literals). Like the good old class literal. These could help in a criteria API. The JPA 2 source code generation work-around for a type-safe query interface could be replaced with it. If it will be part of JDK7.

青巷忧颜 2024-08-19 11:00:11

正如托马斯指出的,这并不严格要求关闭。目前一切都悬而未决,因为没有人确切知道正在考虑什么提案。目前尚不清楚 FCM 是否真的是该提案的基础,特别是考虑到 Stephen Colebourne 似乎和其他人一样对该公告感到惊讶。

很多人都指着 Neal Gafter 神秘地修订了更多或更少的权利作为-Devoxx-presentation-announcing-closures-was-being-given 规范作为闭包可能采取的形式的提示。请注意,修改后的提案(从美学角度)看起来很像 FCM!

该规范确实包含您引用的引用类型(在上一行的“方法引用”下),当然 FCM 也有相同的引用。是的,这肯定会让你的建议成为可能。在阅读本文时,我的第一个想法是它将如何影响 JPA/Hibernate 和/或我们自己的抽象层,在这方面。您的标准中是否引用了类型安全、可重构的方法?是啊。

As Thomas points out, this doesn't strictly require closures. It's all up in the air at the moment, given no-one knows quite what proposal is being looked at. It's not clear if FCM is actually the basis of the proposal, particularly given that Stephen Colebourne seemed to be as susprised as anyone about the announcement.

A lot of people are pointing at Neal Gafter's mysteriously-revised-more-or-less-right-as-the-Devoxx-presentation-announcing-closures-was-being-given spec as a hint as to what form closures might take. Mind you, the revised proposal looks (aesthetically) rather like FCM!

That spec does include the kind of references you refer to (under 'Method References' in the above line), and of course FCM has the same. Yes, this would definitely make you suggest possible. My very first thought when reading about this was how it would affect JPA/Hibernate, and/or our own abstraction layers around same, in this regard. Typesafe, refactorable method references in your criteria? Hell yeah.

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