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 Fuhrmann
Messages: 324
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 Koehnlein
Messages: 656
Registered: July 2009
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
Previous Topic:Xtext - Generating files
Next Topic:Content-Assist for elements in a list
Goto Forum:
  


Current Time: Thu Oct 23 18:06:51 GMT 2014

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

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