Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtend template error:This expression is not allowed in this context, since it doesn't cause any side(xtend template;xtext)
Xtend template error:This expression is not allowed in this context, since it doesn't cause any side [message #1838127] Wed, 17 February 2021 04:24 Go to next message
wings cu is currently offline wings cuFriend
Messages: 5
Registered: January 2021
Junior Member
I'm trying to avoid add a new line when generate code of rule Exp, but i get an error of "This expression is not allowed in this context, since it doesn't cause any side effects."
Why this error will happen? How can I avoid it?
private def Object compile(Exp e) {
		if(e.left !== null) {
			if (e.left instanceof Var){
				'''«(e.left as Var).compile»'''
			}
			else if (e.left instanceof Val) {
				'''«(e.left as Val).compile»'''
			}
			else {
				'''«(e.left as Exp).compile»''' //error 
				if(e.op == '[' || e.op == '(' || e.op == '.')
					'''«e.op»''' //error 
				else 
					''' «e.op» ''' //error
				
				if (e.right !== null)
					'''«(e.right as Exp).compile»'''
			}
		}
	}
Re: Xtend template error:This expression is not allowed in this context, since it doesn't cause any [message #1838129 is a reply to message #1838127] Wed, 17 February 2021 05:41 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13768
Registered: July 2009
Senior Member
'''«(e.left as Exp).compile»''' is an expression that hangs in the air and that does nothing visible to the outside world
maybe you mean something like

'''
«(e.left as Exp).compile»«IF e.op == '[' || e.op == '(' || e.op == '.'»«e.op»«ELSE»«e.op»«ENDIF» // peusdo code
'''


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtend template error:This expression is not allowed in this context, since it doesn't cause any [message #1838144 is a reply to message #1838127] Wed, 17 February 2021 11:57 Go to previous messageGo to next message
wings cu is currently offline wings cuFriend
Messages: 5
Registered: January 2021
Junior Member
I have changed my code as you say, but it still don't work.
I still get a "This expression is not allowed in this context, since it doesn't cause any side effects." error.
'''
«(e.left as Exp).compile»«IF e.op == '[' || e.op == '(' || e.op == '.'»«e.op»«ELSE»«e.op»«ENDIF»
''' //error
Re: Xtend template error:This expression is not allowed in this context, since it doesn't cause any [message #1838152 is a reply to message #1838144] Wed, 17 February 2021 15:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13768
Registered: July 2009
Senior Member
can you please post a complete runnable and self contained java main showing the problem

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtend template error:This expression is not allowed in this context, since it doesn't cause any [message #1838167 is a reply to message #1838144] Thu, 18 February 2021 04:22 Go to previous messageGo to next message
wings cu is currently offline wings cuFriend
Messages: 5
Registered: January 2021
Junior Member
This is my xtext file and xtend file.
I also uploaded my whole project if you need them.
Re: Xtend template error:This expression is not allowed in this context, since it doesn't cause any [message #1838172 is a reply to message #1838167] Thu, 18 February 2021 06:41 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13768
Registered: July 2009
Senior Member
Hi, i dont see "This expression is not allowed in this context, since it doesn't cause any side effects". in the sample code you shared.

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtend template error:This expression is not allowed in this context, since it doesn't cause any [message #1838175 is a reply to message #1838172] Thu, 18 February 2021 07:36 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 31777
Registered: July 2009
Senior Member
Yes, I was confused by the too. One general approach I would suggest is that wherever you see an error about "no side-effect", put a 'return' keyword in front of that expression. Then at least it has a side effect, i.e., it becomes the return value of the method. If you then get "unreachable code" errors then it's pretty clear that you are just executing a statement that has no effect and that you can just delete that statement without changing meaning of the program. In other words, there is no point in using a string literal as a statement unless it is the return value of the method..

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Xtend template error:This expression is not allowed in this context, since it doesn't cause any [message #1838186 is a reply to message #1838167] Thu, 18 February 2021 10:50 Go to previous messageGo to next message
wings cu is currently offline wings cuFriend
Messages: 5
Registered: January 2021
Junior Member
I reopened my project and the error disappered. I'm sorry.

But I still don't understand why this error will happen. I read the java code generated from xtend file. I find this template will generate a string and will then return. So the template code will have side effect but eclipse still give me the error. Is there any details I missed?


And I still have another problem. If I changed my code like this, the code generated by xtext will create new line that I don't need.
a = b

will be generated like
a
=
b

So I have to put the template in the same line.
Is there any way to keep the code readable?
Re: Xtend template error:This expression is not allowed in this context, since it doesn't cause any [message #1838187 is a reply to message #1838186] Thu, 18 February 2021 10:59 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13768
Registered: July 2009
Senior Member
you need to put the <<>> on the same line

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Derivation from OpenApi Specification
Next Topic:Display DSL code generation errors in Eclipse
Goto Forum:
  


Current Time: Thu Mar 04 21:53:05 GMT 2021

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

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

Back to the top