Skip to main content



      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] Tue, 16 February 2021 23:24 Go to next message
Eclipse UserFriend
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 00:41 Go to previous messageGo to next message
Eclipse UserFriend
'''«(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
'''
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 06:57 Go to previous messageGo to next message
Eclipse UserFriend
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 10:03 Go to previous messageGo to next message
Eclipse UserFriend
can you please post a complete runnable and self contained java main showing the problem
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] Wed, 17 February 2021 23:22 Go to previous messageGo to next message
Eclipse UserFriend
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 01:41 Go to previous messageGo to next message
Eclipse UserFriend
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.
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 02:36 Go to previous messageGo to next message
Eclipse UserFriend
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..
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 05:50 Go to previous messageGo to next message
Eclipse UserFriend
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 05:59 Go to previous message
Eclipse UserFriend
you need to put the <<>> on the same line
Previous Topic:Derivation from OpenApi Specification
Next Topic:Display DSL code generation errors in Eclipse
Goto Forum:
  


Current Time: Sat Jul 05 07:23:35 EDT 2025

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

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

Back to the top