[stringtemplate-interest] "Or" and "And" operators in conditionnal always evaluate both operands ?
Sam Harwell
sharwell at pixelminegames.com
Thu Sep 15 07:02:17 PDT 2011
A condition like "a.b" should always evaluate to false if "a" is null. In
StringTemplate, there's no need to condition it on "a" as well. You should
definitely just use this, and if an error is occurring then we should work
it out. :) Can you post the exact condition you're using?
<if(a.b)>
Sam
From: Damien Urruty [mailto:lepilache at hotmail.com]
Sent: Thursday, September 15, 2011 8:54 AM
To: sharwell at pixelminegames.com; stringtemplate-interest at antlr.org
Subject: RE: [stringtemplate-interest] "Or" and "And" operators in
conditionnal always evaluate both operands ?
Hi,
The problem I am facing is that my conditional is something like :
<if(a && a.b)>
Which raises an error in the case a is null (a.b is still evaluated).
By the way, the error is not very explicit : "templateName : no such
property or can't access: null attribute". I spent some time finding where
the error came from.
It is not a question of performance, I was just wondering if this behaviour
was normal, I mean if this is by design, or if it was a bug.
In my case, I can easily break the if in two separate ifs, so it doesn't
really matter. But as in Java the && does not evaluate all the operands if
not necessary (the & operator do evaluate each operand), i wanted to know if
this behaviour was wanted or not.
Maybe this should be discussed, I know it is not very important, or at least
it should be mentionned somewhere in the doc ?
Thanks for your response Sam, have a nice day
_____
From: sharwell at pixelminegames.com
To: lepilache at hotmail.com; stringtemplate-interest at antlr.org
Subject: RE: [stringtemplate-interest] "Or" and "And" operators in
conditionnal always evaluate both operands ?
Date: Thu, 15 Sep 2011 08:43:29 -0500
Hi Damien,
Since the attributes referenced from a StringTemplate should always be pure
(no side effects), then there is no semantic difference between evaluating
or not evaluating the second argument. Are you facing a significant
performance problem due to the second evaluation?
Sam
From: stringtemplate-interest-bounces at antlr.org
[mailto:stringtemplate-interest-bounces at antlr.org] On Behalf Of Damien
Urruty
Sent: Thursday, September 15, 2011 8:01 AM
To: stringtemplate-interest at antlr.org
Subject: [stringtemplate-interest] "Or" and "And" operators in conditionnal
always evaluate both operands ?
Hello,
I can see that if we use && and || operands in conditionnals, both operands
are always evaluated, even if not necessary. For exemple, I have :
<if(a && b)>
...
<endif>
In my case, a is null, so it is not necessary to evaluate the second
argument, right ? Shouldn't the evaluation stop when such a case is
encountered ? It is the same for <if(a || b)> with a not null.
Maybe I miss something, thanks for your help.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20110915/8ed9f100/attachment.html
More information about the stringtemplate-interest
mailing list