You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by "Henning P. Schmiedehausen" <hp...@intermeta.de> on 2006/08/27 19:37:33 UTC

Logical operator gotcha

Just stumbled over that:

--- cut ---

#set ($a = 0)
#set ($b = 1)

#set ($c = $a and $b)
#set ($d = $a or $b)

A: $a
B: $b
C: $c
D: $d

Rendering this template gives a bit of a surprising result:

A: 0
B: 1
C: true
D: true

While the behaviour is obvious (anything not-null is true in Velocity
expressions), it is still sort of a "gotcha". Personally I'd like to
see this behaviour changed (number '0' or the empty String '' get
converted to "false") because then it would match the expression
system of well known languages like perl or php(?).

Any opinions on that?

	Best regards
		Henning

-- 
Dipl.-Inf. (Univ.) Henning P. Schmiedehausen          INTERMETA GmbH
hps@intermeta.de        +49 9131 50 654 0   http://www.intermeta.de/

RedHat Certified Engineer -- Jakarta Turbine Development  -- hero for hire
   Linux, Java, perl, Solaris -- Consulting, Training, Development

Social behaviour: Bavarians can be extremely egalitarian and folksy.
                                    -- http://en.wikipedia.org/wiki/Bavaria
Most Franconians do not like to be called Bavarians.
                                    -- http://en.wikipedia.org/wiki/Franconia

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org


Re: Logical operator gotcha

Posted by Llewellyn Falco <is...@setgame.com>.
-1
i personally like that booleans are booleans in java.

int a,b
if (a || b) ;

won't compile, and shouldn't compile. if you want to know if they're 0 write 
it out,

the fact that

if (a = b);

won't compile has saved many more problems than it has created.

i would be ok  pushing this up into velocity also, if i'm doing boolean 
logic on non boolean objects, throw an exception; this keeps it clean, and 
avoids the gottcha of the current method.

    llewellyn. 


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org


Re: Logical operator gotcha

Posted by Will Glass-Husain <wg...@forio.com>.
I'm against changing this.

I've recently encountered this issue in PHP, where 0, false, empty
string and null all fail an if condition.  I was testing to see if an
object was null, but it was 0 so it failed.  (when it should have
passed).  Took me a while to figure out why my if condition was
failing.

If you have booleans and ints but no nulls having 0 == false makes
sense, they are two separate things.  But when you also have nulls, I
think that having a "test for null" be the same as "test for 0" is a
little confusing.

WILL


On 8/27/06, Henning P. Schmiedehausen <hp...@intermeta.de> wrote:
> Just stumbled over that:
>
> --- cut ---
>
> #set ($a = 0)
> #set ($b = 1)
>
> #set ($c = $a and $b)
> #set ($d = $a or $b)
>
> A: $a
> B: $b
> C: $c
> D: $d
>
> Rendering this template gives a bit of a surprising result:
>
> A: 0
> B: 1
> C: true
> D: true
>
> While the behaviour is obvious (anything not-null is true in Velocity
> expressions), it is still sort of a "gotcha". Personally I'd like to
> see this behaviour changed (number '0' or the empty String '' get
> converted to "false") because then it would match the expression
> system of well known languages like perl or php(?).
>
> Any opinions on that?
>
>         Best regards
>                 Henning
>
> --
> Dipl.-Inf. (Univ.) Henning P. Schmiedehausen          INTERMETA GmbH
> hps@intermeta.de        +49 9131 50 654 0   http://www.intermeta.de/
>
> RedHat Certified Engineer -- Jakarta Turbine Development  -- hero for hire
>    Linux, Java, perl, Solaris -- Consulting, Training, Development
>
> Social behaviour: Bavarians can be extremely egalitarian and folksy.
>                                     -- http://en.wikipedia.org/wiki/Bavaria
> Most Franconians do not like to be called Bavarians.
>                                     -- http://en.wikipedia.org/wiki/Franconia
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
>
>


-- 
Forio Business Simulations

Will Glass-Husain
wglass@forio.com
www.forio.com

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org


Re: Logical operator gotcha

Posted by Geir Magnusson Jr <ge...@pobox.com>.
+1 to the -1 :)

(I agree)


Nathan Bubna wrote:
> Gotta say, i'm -1 on this too.  zero isn't nothing to me. :)  i could
> easily see this wreaking havoc on some of my templates.
> 
> On 8/27/06, Henning P. Schmiedehausen <hp...@intermeta.de> wrote:
>> Just stumbled over that:
>>
>> --- cut ---
>>
>> #set ($a = 0)
>> #set ($b = 1)
>>
>> #set ($c = $a and $b)
>> #set ($d = $a or $b)
>>
>> A: $a
>> B: $b
>> C: $c
>> D: $d
>>
>> Rendering this template gives a bit of a surprising result:
>>
>> A: 0
>> B: 1
>> C: true
>> D: true
>>
>> While the behaviour is obvious (anything not-null is true in Velocity
>> expressions), it is still sort of a "gotcha". Personally I'd like to
>> see this behaviour changed (number '0' or the empty String '' get
>> converted to "false") because then it would match the expression
>> system of well known languages like perl or php(?).
>>
>> Any opinions on that?
>>
>>         Best regards
>>                 Henning
>>
>> -- 
>> Dipl.-Inf. (Univ.) Henning P. Schmiedehausen          INTERMETA GmbH
>> hps@intermeta.de        +49 9131 50 654 0   http://www.intermeta.de/
>>
>> RedHat Certified Engineer -- Jakarta Turbine Development  -- hero for
>> hire
>>    Linux, Java, perl, Solaris -- Consulting, Training, Development
>>
>> Social behaviour: Bavarians can be extremely egalitarian and folksy.
>>                                     --
>> http://en.wikipedia.org/wiki/Bavaria
>> Most Franconians do not like to be called Bavarians.
>>                                     --
>> http://en.wikipedia.org/wiki/Franconia
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
> 
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org


Re: Logical operator gotcha

Posted by Henning Schmiedehausen <hp...@intermeta.de>.
Well, fine with me. That's why I was asking. I will make sure that it is
right in the User Guide.

	Best regards
		Henning


On Sun, 2006-08-27 at 13:36 -0700, Nathan Bubna wrote:
> Gotta say, i'm -1 on this too.  zero isn't nothing to me. :)  i could
> easily see this wreaking havoc on some of my templates.
> 
> On 8/27/06, Henning P. Schmiedehausen <hp...@intermeta.de> wrote:
> > Just stumbled over that:
> >
> > --- cut ---
> >
> > #set ($a = 0)
> > #set ($b = 1)
> >
> > #set ($c = $a and $b)
> > #set ($d = $a or $b)
> >
> > A: $a
> > B: $b
> > C: $c
> > D: $d
> >
> > Rendering this template gives a bit of a surprising result:
> >
> > A: 0
> > B: 1
> > C: true
> > D: true
> >
> > While the behaviour is obvious (anything not-null is true in Velocity
> > expressions), it is still sort of a "gotcha". Personally I'd like to
> > see this behaviour changed (number '0' or the empty String '' get
> > converted to "false") because then it would match the expression
> > system of well known languages like perl or php(?).
> >
> > Any opinions on that?
> >
> >         Best regards
> >                 Henning
> >
> > --
> > Dipl.-Inf. (Univ.) Henning P. Schmiedehausen          INTERMETA GmbH
> > hps@intermeta.de        +49 9131 50 654 0   http://www.intermeta.de/
> >
> > RedHat Certified Engineer -- Jakarta Turbine Development  -- hero for hire
> >    Linux, Java, perl, Solaris -- Consulting, Training, Development
> >
> > Social behaviour: Bavarians can be extremely egalitarian and folksy.
> >                                     -- http://en.wikipedia.org/wiki/Bavaria
> > Most Franconians do not like to be called Bavarians.
> >                                     -- http://en.wikipedia.org/wiki/Franconia
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
> >
> >
-- 
Dipl.-Inf. (Univ.) Henning P. Schmiedehausen          INTERMETA GmbH
hps@intermeta.de        +49 9131 50 654 0   http://www.intermeta.de/

      RedHat Certified Engineer -- Jakarta Turbine Development
   Linux, Java, perl, Solaris -- Consulting, Training, Engineering

"For a successful technology, reality must take precedence over
 public relations for Nature cannot be fooled" - Richard P. Feynman


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org


Re: Logical operator gotcha

Posted by Nathan Bubna <nb...@gmail.com>.
Gotta say, i'm -1 on this too.  zero isn't nothing to me. :)  i could
easily see this wreaking havoc on some of my templates.

On 8/27/06, Henning P. Schmiedehausen <hp...@intermeta.de> wrote:
> Just stumbled over that:
>
> --- cut ---
>
> #set ($a = 0)
> #set ($b = 1)
>
> #set ($c = $a and $b)
> #set ($d = $a or $b)
>
> A: $a
> B: $b
> C: $c
> D: $d
>
> Rendering this template gives a bit of a surprising result:
>
> A: 0
> B: 1
> C: true
> D: true
>
> While the behaviour is obvious (anything not-null is true in Velocity
> expressions), it is still sort of a "gotcha". Personally I'd like to
> see this behaviour changed (number '0' or the empty String '' get
> converted to "false") because then it would match the expression
> system of well known languages like perl or php(?).
>
> Any opinions on that?
>
>         Best regards
>                 Henning
>
> --
> Dipl.-Inf. (Univ.) Henning P. Schmiedehausen          INTERMETA GmbH
> hps@intermeta.de        +49 9131 50 654 0   http://www.intermeta.de/
>
> RedHat Certified Engineer -- Jakarta Turbine Development  -- hero for hire
>    Linux, Java, perl, Solaris -- Consulting, Training, Development
>
> Social behaviour: Bavarians can be extremely egalitarian and folksy.
>                                     -- http://en.wikipedia.org/wiki/Bavaria
> Most Franconians do not like to be called Bavarians.
>                                     -- http://en.wikipedia.org/wiki/Franconia
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org