lambdaj 闭包中使用的 Java 机制

发布于 2024-08-08 23:19:17 字数 532 浏览 7 评论 0原文

Lamdbaj允许在Java语言中定义闭包,可以找到各种例子 此处

我的问题是关于使用的底层 Java 机制,例如定义 println 闭包,使用以下代码:

Closure println = closure(); 
{ of(System.out).println(var(String.class)); }

该闭包随后可以通过以下方式执行:

println.apply("foobar");

我很好奇 Java 中的哪些机制允许调用 of(...)。 println(...)println 实例本身关联。

当然,lambdaj 源代码是可以阅读的,但我希望如果有人有的话,能得到一个稍微更高层次的解释。我的反思技能包括一些内省和动态执行方法。

Lamdbaj allows the definition of closures in the Java language, various examples can be found
here

My question is regarding the underlying Java mechanisms at use, for instance, to define the println closure, the following code is used:

Closure println = closure(); 
{ of(System.out).println(var(String.class)); }

This closure can be subsequently executed via:

println.apply("foobar");

I am curious as to what mechanisms in Java would allow the call to of(...).println(...) to become associated with the println instance itself.

Naturally, the lambdaj source code is available to read but I was hoping for a slightly higher level explanation if anyone has one. My reflection skills go as far as a bit of introspection and executing methods dynamically.

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

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

发布评论

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

评论(2

生来就爱笑 2024-08-15 23:19:17

我是 Mario Fusco,是 lambdaj 库的主要开发人员。

首先我想澄清一点:lambdaj 并不是要取代任何函数式语言。正如我上周在 Zurich Jug 的演讲中所说,如果您有机会使用 Scala,那就放手一搏,永不回头。您可以在这里找到我的演讲简历,其中明确指出:

http://ctpjava.blogspot.com/2009/10/lambdaj-new-trends-in-java.html

我也是一名快乐的 Scala 开发人员。但有时您不得不使用 Java 进行开发(根据我的经验,在现实世界中,大约 80% 的情况下您无法选择必须使用哪种语言编写代码),在这种情况下,某些 lambdaj 功能可能是有帮助(或者我希望如此)。我只是想为 Java 带来一些完全缺失的功能特性。当然,结果并不完全令人满意,主要是由于Java本身的限制。

至于内部 lambdaj 机制,是的,它使用 ThreadLocal 来实现该结果。如果您对 lambdaj 有其他问题、好奇心或者更好的建议和建设性批评,也许您有兴趣在此处注册到 lambdaj 邮件列表:

http://groups.google.com/group/lambdaj

再见
马里奥

I am Mario Fusco and I am the main developer of the lambdaj library.

First of all I would like to clarify something: lambdaj is not intended to replace any functional language. As I said last week in my speech at the Jug of Zurich if you have a chance to use Scala, go for it and never look back. Here you can find a resume of my speech where it is clearly stated that:

http://ctpjava.blogspot.com/2009/10/lambdaj-new-trends-in-java.html

I am an happy Scala developer too. But sometimes you are just obliged to develop in Java (in my experience, in the real world, about the 80% of times you cannot choose in which language you have to write your code) and in this case some of the lambdaj features could be helpful (or I hope so). I just wanted to bring to Java some functional features that are totally missing. Of course the result is not completely satisfying mainly due to the limitation imposed by Java itself.

As for the internal lambdaj mechanism, yes it uses a ThreadLocal in order to achieve that result. If you have other questions, curiosities or even better suggestions and constructive critics about lambdaj maybe you could be interested to register yourself to the lambdaj mailing list here:

http://groups.google.com/group/lambdaj

Bye
Mario

晌融 2024-08-15 23:19:17

嗯,of 大概是一个static 方法,它是静态导入的,因此可以在没有封闭类名的情况下调用它。我希望 var 是相同的。两种方法都必须返回某种类型,该类型具有随后调用的方法:

public class Printable {
  public void println(Var var);
}

public class Fac {
  public static Printable of(Object o) {
    return new Printable(o);
  }

  public static Var var(Class<?> clazz) {
    return new Var(clazz);
  }

} 

突然:

Fac.of(System.out).println(Fac.var(String.class));

是有效的 Java。使用静态导入,嘿,快点:

import static Fac.*;

of(System.out).println(var(String.class));

花括号显然是有效的 Java,因为您可以在任何方法中添加它们以帮助定义词法 sope。这种 API 设计风格称为 fluid,最好的展示方式是JMock 测试库。

顺便说一句,如果这应该向 Java 引入闭包,那就太荒谬了 - 语法极其糟糕。他们的 I/O 示例确实让我笑出了声。尝试Scala

编辑 - 我相信这两个 println 调用是相关的,因为第一个调用序列允许库捕获您作为参数传入的变量。这些可能被捕获在一些ThreadLocal结构中。然后,当您调用(也可能是静态的)println 方法时,该库将使用捕获的数据在稍后实际执行该行为。同样与测试相关的是,EasyMock 测试框架使用类似的机制(在后台使用 Java 代理)来捕获期望值。

Well, of is presumably a static method which is imported statically so it can be called without the enclosing class name. I expect that var is the same. Both methods must return some type which have the methods subsequently called:

public class Printable {
  public void println(Var var);
}

public class Fac {
  public static Printable of(Object o) {
    return new Printable(o);
  }

  public static Var var(Class<?> clazz) {
    return new Var(clazz);
  }

} 

All of a sudden:

Fac.of(System.out).println(Fac.var(String.class));

Is valid Java. Using static imports, hey presto:

import static Fac.*;

of(System.out).println(var(String.class));

The curly-braces are obviously valid Java as you can add these in any method to aid in defining a lexical sope. This API-design style is called fluent and is best showcased by the JMock testing library.

By the way, if this is supposed to introduce closures to Java, it's quite ridiculous - the syntax is unreadably awful. Their I/O example actually made me laugh out loud. Try Scala!

EDIT - the two println calls are associated I believe because the first sequence of calls allow the library to capture the variables which you have passed in as parameters. These are probably captured in some ThreadLocal structure. When you then call a (also presumably static) println method, the library is using this captured data to actually execute the behaviour at a later point. Also testing related, the EasyMock test framework uses a similar mechanism (which uses Java proxies in the background) to capture expected values.

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