Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtend: How to create a local variable
Xtend: How to create a local variable [message #1077102] Thu, 01 August 2013 08:50 Go to next message
Chris Hansen is currently offline Chris HansenFriend
Messages: 20
Registered: July 2013
Junior Member
Hello,

it is a really dumb question:

How do I declare re-define a local variable in Xtend?
My try:


def Foo(String filterPath, String filterName)
'''
«var fullFilterName = ""»
«IF filterPath.length() > 0»
«fullFilterName = filterPath + "." + filterName»
«ELSE»
«fullFilterName = filterName»
«ENDIF»
'''

The result is that the generated Code contains the value of "fullFilterName", which I don't want to get printed out here.

I can't figure out how I can prevent it from being printed automatically.


Thanks,
Chris
Re: Xtend: How to create a local variable [message #1077133 is a reply to message #1077102] Thu, 01 August 2013 09:32 Go to previous messageGo to next message
Hallvard Traetteberg is currently offline Hallvard TraettebergFriend
Messages: 673
Registered: July 2009
Location: Trondheim, Norway
Senior Member
Chris,

You seem to be using template strings without any reason. Just write
ordinary Xtend with var if, else etc. directly instead of embedding it
within ''' and escaping with << and >>

def Foo(String filterPath, String filterName) {
var fullFilterName = ""
if (filterPath.length() > 0)
fullFilterName = filterPath + "." + filterName
else
fullFilterName = filterName
...
}

Hallvard

On 01.08.13 10.50, Chris Hansen wrote:
> Hello,
>
> it is a really dumb question:
>
> How do I declare re-define a local variable in Xtend?
> My try:
>
>
> def Foo(String filterPath, String filterName)
> '''
> «var fullFilterName = ""»
> «IF filterPath.length() > 0»
> «fullFilterName = filterPath + "." + filterName»
> «ELSE»
> «fullFilterName = filterName»
> «ENDIF» '''
>
> The result is that the generated Code contains the value of
> "fullFilterName", which I don't want to get printed out here.
>
> I can't figure out how I can prevent it from being printed automatically.
>
>
> Thanks,
> Chris
Re: Xtend: How to create a local variable [message #1077239 is a reply to message #1077133] Thu, 01 August 2013 12:19 Go to previous messageGo to next message
Chris Hansen is currently offline Chris HansenFriend
Messages: 20
Registered: July 2013
Junior Member
The reason why I used Templates is:

When trying to mix Templates into the Xtend Code, the Error "This expression is not allowed in this context, since it doesn't cause any side effects." tends to occur.

Am I not allowed to use Templates within a for-Loop?

for(use : monitor.use)
{
	if(use.measinstance != null)
	{		
		'''«printAllFiltersMeasinstance(use.measinstance.type, use.measinstance.name, suffix)»'''                      << Causes the Error
	}
}


[Updated on: Thu, 01 August 2013 12:21]

Report message to a moderator

Re: Xtend: How to create a local variable [message #1077242 is a reply to message #1077239] Thu, 01 August 2013 12:24 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
hmmm looks like a

monitor.use.filterNull().map[printAllFiltersMeasinstance(use.measinstance.type, use.measinstance.name, suffix)].join()

(may contain a typo)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtend: How to create a local variable [message #1077341 is a reply to message #1077242] Thu, 01 August 2013 15:08 Go to previous messageGo to next message
Chris Hansen is currently offline Chris HansenFriend
Messages: 20
Registered: July 2013
Junior Member
The same thing happens here:

def foo()
{
   ''' SOMETHING '''

   functionCall();
}


But this version works:

def foo()
{
   funcCall();
   ''' SOMETHING'''
}


Maybe there is a Bug?
Re: Xtend: How to create a local variable [message #1077363 is a reply to message #1077341] Thu, 01 August 2013 15:44 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Nope it is a feature I think. Xtends wants to make you program side
effect free

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Using maven to test an Xtext DSL to Java generator
Next Topic:adding file operations
Goto Forum:
  


Current Time: Wed Apr 24 22:43:12 GMT 2024

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

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

Back to the top