[Xtend 2.3 M6] Function1 cannot be used as parameter [message #876731] |
Fri, 25 May 2012 07:56 |
Kevin Wiedener Messages: 9 Registered: May 2012 |
Junior Member |
|
|
Hi there,
I'm currently playing around with Xtend2 and it seems to be a very cool and innovative project. In order to replace the old
EXPAND function FOREACH sampleList
construct from Xpand I would like to build a similar syntax with Xtend2.
For now the only way to do it is to call
sampleList.map([function]).join
which is annoying me. I wonder why there isn't an extension for such often used constructs (or am I doing it the wrong way?). I would have expected an extension for lists in that case, but it doesn't seem to be there.
I have tried to simplify the map-join syntax. Here is my first approach (it is not compiling!):
import org.eclipse.emf.common.util.EList
import org.eclipse.xtext.xbase.lib.Functions.Function1
def <T> String toString(EList<T> value, Function1<T, CharSequence> func)
{
return value.map(func).join
}
The whole thing crashes because it seems to be impossible to import Function1 and use it as parameter. Anyone has an idea why this is the case?
I tried another solution but it didn't work either because the code is generated in a way, that doesn't work with my solution:
import org.eclipse.emf.common.util.EList
import org.eclipse.xtext.xbase.lib.internal.FunctionDelegate
import com.google.common.collect.Lists
def <T> String toString(EList<T> value, FunctionDelegate<T, CharSequence> func)
{
return Lists::transform(value, func).join;
}
Nevertheless with a helper class it is easy to add a workaround, but I wonder why I need to do it in plain java. For all who are interested in: here is the code of the helper class and an Xtend2 example how it can be used in a way that fits my needs (thanks to the "it" feature of Xtend2 ;)):
import java.util.List;
import org.eclipse.emf.common.util.EList;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ListExtensions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
public class Helper {
public static <T> String toString(EList<T> value, Function1<T, CharSequence> func)
{
List<Object> _map = ListExtensions.<T, Object>map(value, func);
String _join = IterableExtensions.join(_map);
return _join;
}
}
def private printExample(ParentType parent)
{
'''
«allChilds.toString([generateChildContent])»
'''
}
def private generateChildContent(ChildType it)
{
// this method doesn't make much sense, but who cares ;)
return name
}
Well, maybe the next release M8 of Xtend2 extends the existing ListExtensions class with such a toString or output method.
[Updated on: Fri, 25 May 2012 08:06] Report message to a moderator
|
|
|
|
|
|
Re: Xtend2: Generic dispatch method does not compile [message #876767 is a reply to message #876752] |
Fri, 25 May 2012 08:56 |
|
Hi, i do net get your point
package org.xtext.example.mydsl
import org.eclipse.emf.common.util.EList
import org.eclipse.xtext.xbase.lib.Functions$Function1
class Helper {
def <T> String toString(EList<T> value, Functions$Function1<T, CharSequence> func)
{
return value.map(func).join
}
def <T> String toString2(EList<T> value, (T) => CharSequence func)
{
return value.map(func).join
}
}
~Christian
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
Powered by
FUDForum. Page generated in 0.02003 seconds