简体   繁体   English

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

[英]closures mean fully type-safe criteria?

combining closures (FCM) and generics, would it be possible to have fully type-safe criteria. 结合使用闭包(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();

I've read the JPA 2.0 spec for type-safe criteria. 我已经阅读了有关类型安全标准的JPA 2.0规范。 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. 我大量使用了Java的静态类型,以便让我更快地进行编码。 But as a result I get bitten now and then by the parts of my code that ignore typing. 但是结果是我不时被那些忽略键入的代码所咬住。 For example HQL queries. 例如HQL查询。

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. 这些可能有助于标准API。 The JPA 2 source code generation work-around for a type-safe query interface could be replaced with it. 类型安全查询接口的JPA 2源代码生成替代方法可以用它替代。 If it will be part of JDK7. 如果它将成为JDK7的一部分。

As Thomas points out, this doesn't strictly require closures. 正如Thomas所指出的,这并不严格要求关闭。 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. 尚不清楚FCM是否实际上是该提案的基础,尤其是考虑到斯蒂芬·科尔本(Stephen Colebourne)似乎对任何人都对该公告感到惊讶。

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. 很多人指出, Neal Gafter神秘地修改了Devoxx演示文稿宣布关闭的名称,或多或少的正确性 ,以作为关闭格式的提示。 Mind you, the revised proposal looks (aesthetically) rather like FCM! 提醒您,修订后的提案从美学上看看起来像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. 该规范确实包括您所引用的引用类型(在上一行的“方法引用”下),当然FCM也具有相同的引用。 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. 在阅读此内容时,我的第一个念头是,这将如何影响JPA / Hibernate和/或我们自己周围的抽象层。 Typesafe, refactorable method references in your criteria? 您的条件中是否有类型安全,可重构的方法引用? Hell yeah. 真是的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM