Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtend templates execution : unable to print return value of a method when used in forEach(Xtend template execution)
Xtend templates execution : unable to print return value of a method when used in forEach [message #1781274] Mon, 05 February 2018 12:21 Go to next message
Zakir Meer is currently offline Zakir MeerFriend
Messages: 50
Registered: February 2016
Member
Hello Experts,

I have a query w.r.t. extracting the return value of a method , when used with forEach on a list/iterator to generate the content from templates.

To simplify my query, I have written a small program -> here printElementsValue method is executed for each element in a list, but unfortunately I am not able to extract the result of printElementsValue which was executed as a part of forEach statement

import java.util.List
import java.util.ArrayList

class HelloWorld {
	def static void main(String[] args) {
		
		var List<String> ls=new ArrayList
		
		ls.add("one")
		ls.add("two")
		ls.add("three")
		
		var CharSequence cs=printValues(ls)
		
		println(cs.toString)
	}
	
	def static printValues(List<String> ls)'''
	Printing contents of list
««« how to append the return value of method printElementsValue?
		«ls.forEach[printElementsValue]»
	'''
	
	def static printElementsValue(String st)'''
	Element in list is: «st»
	'''
}


I will be happy if someone can let me know how to fetch the return value of a method when used in combination with forEach

Thanks !!
Re: Xtend templates execution : unable to print return value of a method when used in forEach [message #1781277 is a reply to message #1781274] Mon, 05 February 2018 12:28 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
is there any reason not to use the template for loop?

«FOR it : ls»«printElementsValue»«ENDFOR»


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtend templates execution : unable to print return value of a method when used in forEach [message #1781279 is a reply to message #1781277] Mon, 05 February 2018 12:58 Go to previous messageGo to next message
Zakir Meer is currently offline Zakir MeerFriend
Messages: 50
Registered: February 2016
Member
Thanks for your quick response !!
In my original case, I have a Iterator of which elements should be fetched in a loop. Unfortunately I am missing while loop in the templates.

When I use FOR loop in the below way, I get following compilation error: Type mismatch: cannot convert from ExtendedIterator<OntClass> to Iterable<?>
	«FOR it:  listClassesIterator»
			««« add content
			«ENDFOR»


Due to this reason I switched to forEach on iterator, and unfortunately with forEach I am not able get return values after method is processed :-(
Re: Xtend templates execution : unable to print return value of a method when used in forEach [message #1781280 is a reply to message #1781279] Mon, 05 February 2018 13:01 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
you can use map operator and join

«ls.map[printElementsValue].join("")»

or use iterator.toIterable


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtend templates execution : unable to print return value of a method when used in forEach [message #1781281 is a reply to message #1781280] Mon, 05 February 2018 13:07 Go to previous message
Zakir Meer is currently offline Zakir MeerFriend
Messages: 50
Registered: February 2016
Member
thanks a lot :-) .. it works for me with «ls.map[printElementsValue].join("")»
Previous Topic:Conflicting Regions for new line
Next Topic:How to generate a factory?
Goto Forum:
  


Current Time: Thu Mar 28 21:36:29 GMT 2024

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

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

Back to the top