Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [Xtend 2.3 M6] Function1 cannot be used as parameter
[Xtend 2.3 M6] Function1 cannot be used as parameter [message #876731] Fri, 25 May 2012 07:56 Go to next message
Kevin Wiedener is currently offline Kevin WiedenerFriend
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 #876738 is a reply to message #876731] Fri, 25 May 2012 08:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
Hi you Import nested classes with Outer$Inner

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtend2: Generic dispatch method does not compile [message #876752 is a reply to message #876738] Fri, 25 May 2012 08:33 Go to previous messageGo to next message
Kevin Wiedener is currently offline Kevin WiedenerFriend
Messages: 9
Registered: May 2012
Junior Member
Thanks Christian, so that's not supported in Xtend2? Any reasons why that's possible in java but not supported in Xtend2?
Re: Xtend2: Generic dispatch method does not compile [message #876764 is a reply to message #876752] Fri, 25 May 2012 08:51 Go to previous messageGo to next message
Vlad Dumitrescu is currently offline Vlad DumitrescuFriend
Messages: 431
Registered: July 2009
Location: Gothenburg
Senior Member
As Christian said, use
import org.eclipse.xtext.xbase.lib.Functions$Function1

or the more xtend-y syntax
	def static <T> String toString(EList<T> value, (T)=>CharSequence func)
	{
		value.map(func).join
	}


Re: Xtend2: Generic dispatch method does not compile [message #876767 is a reply to message #876752] Fri, 25 May 2012 08:56 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
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
Re: Xtend2: Generic dispatch method does not compile [message #876840 is a reply to message #876767] Fri, 25 May 2012 11:15 Go to previous message
Kevin Wiedener is currently offline Kevin WiedenerFriend
Messages: 9
Registered: May 2012
Junior Member
Thanks guys, I've misunderstood Christians comment. I haven't found any documentation about the importing of inner classes. Everything works fine now.
Previous Topic:importURI strange problems
Next Topic:Cross-Reference
Goto Forum:
  


Current Time: Tue Apr 16 04:52:59 GMT 2024

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

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

Back to the top