Community
Participate
Working Groups
Build Identifier: My motivation is really to get xtend closures working seamlessly with FJ's functions, which, unfortunately, are abstract classes, instead of interfaces. I have a patch that seems to work for FJ functions. Reproducible: Always Steps to Reproduce: After the enhancement, this should print false: import fj.F class Test { def static void main(String[] args) { val F<Integer, Integer, ?> f = [x, y | x + y > 1] println(f.f(1, 0)) } }
Created attachment 210193 [details] Change to allow xtend closures to coerce to SAM classes I tried to keep the changes minimal and follow the coding philosophy of the project as best I could.
We'd have to rethink the implementation of the interpreter in order to allow closures for SAM classes.
(In reply to comment #2) > We'd have to rethink the implementation of the interpreter in order to allow > closures for SAM classes. Well we could do this for Xtend only and stick with interfaces for the general expression lib.
After internal discussion we agreed on doing it in Xbase but having it limited to interfaces by default. Languages need explicitly enable support for abstract classes, by overriding a small template method. Also we want to have a validation rule, which replaces the generic type conformance checks telling the user, that the expected type 'Foo' is not a functional type.
Thoughtworks' technology radar motivated me to have a look at Functional Java. Then I remembered this ticket... Here's the SAM: http://functionaljava.googlecode.com/svn/artifacts/3.0/javadoc/fj/F.html
I'd love to see this change make it into Xtend. Im currently doing a GWT + FJ project and it would be great to combine XTend's lambda syntax with FJ. Kudos to Bill for his patch too! Not a trivial undertaking for a user. If this patch still works against latest Xtend, and its pretty easy to build, I might just patch my Xtend to get this working...
*** Bug 389597 has been marked as a duplicate of this bug. ***
+1 Guava also has some SAMs, e.g. CacheLoader or AbstractIterator.
Implemented first draft of sam support for abstract classes. Allows to implement the sample abstract iterator from guava public static Iterator skipNulls(final Iterator in) { return new AbstractIterator() { protected String computeNext() { while (in.hasNext()) { String s = in.next(); if (s != null) { return s; } } return endOfData(); } }; }} can be implemented like this: def <T> Iterator<T> skipNulls(Iterator<T> iter) { val AbstractIterator<T> result = [| while(iter.hasNext) { val elem = iter.next if (elem != null) return elem } return self.endOfData ] return result } or it could even be implemented like this if elvis would be evaluated lazily: def <T> Iterator<T> skipNulls(Iterator<T> it) { val AbstractIterator<T> result = [| iter.findFirst [ it != null ] ?: self.endOfData ] return result } where self is the pointer to 'this' closure type, e.g. it allows to invoke endOfData. Note that it is not necessary to have a #super for the closure type since only a single abstract method can be implemented like that (which does not have a super implementation) and all other members can be reached by self.<member> Will be released together with the new type system. Remaining issues shall be tracked in individual tickets.
Requested via bug 522520. -M.