[stringtemplate-interest] "Or" and "And" operators in conditionnal always evaluate both operands ?
Sam Harwell
sharwell at pixelminegames.com
Thu Sep 15 07:50:33 PDT 2011
It's important to realize that the dynamic attribute lookup uses reflection.
In the case of the expression "type.defaultConstructor.exceptions", if
"type.defaultConstructor" evaluates to null then it's impossible to resolve
or evaluate the property "exceptions", so the conditional is simply false.
The only way "type.defaultConstructor.exceptions" should produce an error
instead of false is if getDefaultConstructor() or getExceptions() is
throwing an exception.
Your condition should read <if(type.defaultConstructor.exceptions)>.
The message you are seeing is due to a current inability for ST4 to
distinguish between the following two expressions:
<if(type.defaultConstructor.exceptions)>
<type.defaultConstructor.exceptions>
I believe 2 new bytecodes (marked with *) need to be added and used as
follows. Note that the semantics would change for the existing opcodes.
. INSTR_LOAD_PROP, used for <if(...)>: Returns null if the source
object is null.
. *INSTR_LOAD_PROP_CHECKED, used for <...>: Results in a runtime
error if the source object is null.
. INSTR_LOAD_PROP_IND, used for <if(a.(...))>: Returns null if
either the source object is null or the property name is null/empty.
. *INSTR_LOAD_PROP_IND_CHECKED, used for <a.(...)>: Runtime error if
source object is null or property name is null/empty.
Sam
From: Damien Urruty [mailto:lepilache at hotmail.com]
Sent: Thursday, September 15, 2011 9:19 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 ?
The case I use is the following :
<if(type.defaultConstructor && type.defaultConstructor.exceptions)>
with the first operand calling the getDefaultConstructor() method on my type
object, and the second operand calling getExceptions() on my constructor
object, returning a list of types raised byt eh constructor.
If there is no default constructor declared in my type, then the second
chunk is still evaluated, and gives me the error given in the previous mail
:
"templateName : no such property or can't access: null attribute"
So what you are saying Sam, is that I could use this conditional (even with
the two operand), and this should not raise any error ?
_____
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 09:02:17 -0500
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/39ad14de/attachment-0001.html
More information about the stringtemplate-interest
mailing list