Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [Xtend2]
[Xtend2] [message #1016354] Wed, 06 March 2013 07:47 Go to next message
Hauke Fuhrmann is currently offline Hauke FuhrmannFriend
Messages: 333
Registered: July 2009
Senior Member
Hi there,

I'm struggling a bit with lambda expressions, see
http://www.eclipse.org/xtend/documentation.html#lambdas

I understand that a lambda expression is an anonymous function that I
can pass as argument to methods that accept functions.

However, the examples there use standard Java methods, like
addActionListener that take an object as argument, i.e. an Object of
type ActionListener. This is usually implemented as anonymous Object
implementing the only method actionPerformed.

In the Xtend-pendant there is only a closure that implements the
behavior of the actionPerformed method. However, it is not named nor is
there any object wrapping the function like the required ActionListener.

So how are the rules that Xtend infers all these required informations?
Where are the limits? Is this only possible for anonymous classes with
exactly one method?

My own example is the FileVisitor of Java 7:
http://docs.oracle.com/javase/7/docs/api/java/nio/file/FileVisitor.html
(see below)

Is it possible to implement the pattern in the first example there with
lambda expressions? How is the SimpleFileVisitor replaced?

Something like

Files::walkFileTree(path, [Path file, BasicFileAttributes attrs|
println(file)])
?

Cheers,
Hauke



File Visitor pattern in Java:

Path start = ...
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs)
throws IOException
{
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir,
IOException e)
throws IOException
{
if (e == null) {
Files.delete(dir);
return FileVisitResult.CONTINUE;
} else {
// directory iteration failed
throw e;
}
}
});
Re: [Xtend2] [message #1020582 is a reply to message #1016354] Mon, 18 March 2013 13:54 Go to previous message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 760
Registered: July 2009
Location: Hamburg
Senior Member
Java uses interfaces with a single method where other languages use
lambda expressions. This is why Xtend will coerce a lambda to an
interface (or class in Xtend 2.4) with a single abstract method if
needed. The methods from java.lang.Object don't count (that's why
Comparable<T> can be implemented with a lambda)

FileVisitor and SimpleFileVisitor wouldn't work, as they have 4/0
abstract methods.

Am 06.03.13 08:47, schrieb Hauke Fuhrmann:
> Hi there,
>
> I'm struggling a bit with lambda expressions, see
> http://www.eclipse.org/xtend/documentation.html#lambdas
>
> I understand that a lambda expression is an anonymous function that I
> can pass as argument to methods that accept functions.
>
> However, the examples there use standard Java methods, like
> addActionListener that take an object as argument, i.e. an Object of
> type ActionListener. This is usually implemented as anonymous Object
> implementing the only method actionPerformed.
>
> In the Xtend-pendant there is only a closure that implements the
> behavior of the actionPerformed method. However, it is not named nor is
> there any object wrapping the function like the required ActionListener.
>
> So how are the rules that Xtend infers all these required informations?
> Where are the limits? Is this only possible for anonymous classes with
> exactly one method?
>
> My own example is the FileVisitor of Java 7:
> http://docs.oracle.com/javase/7/docs/api/java/nio/file/FileVisitor.html
> (see below)
>
> Is it possible to implement the pattern in the first example there with
> lambda expressions? How is the SimpleFileVisitor replaced?
>
> Something like
>
> Files::walkFileTree(path, [Path file, BasicFileAttributes attrs|
> println(file)])
> ?
>
> Cheers,
> Hauke
>
>
>
> File Visitor pattern in Java:
>
> Path start = ...
> Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
> @Override
> public FileVisitResult visitFile(Path file,
> BasicFileAttributes attrs)
> throws IOException
> {
> Files.delete(file);
> return FileVisitResult.CONTINUE;
> }
> @Override
> public FileVisitResult postVisitDirectory(Path dir,
> IOException e)
> throws IOException
> {
> if (e == null) {
> Files.delete(dir);
> return FileVisitResult.CONTINUE;
> } else {
> // directory iteration failed
> throw e;
> }
> }
> });


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com


---
Get professional support from the Xtext committers at www.typefox.io
Previous Topic:Xtext - Generating files
Next Topic:Content-Assist for elements in a list
Goto Forum:
  


Current Time: Thu Mar 28 20:12:24 GMT 2024

Powered by FUDForum. Page generated in 0.02140 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top