You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Tal Dayan <ta...@zapta.com> on 2001/07/14 03:42:34 UTC

Integers and numbers in a comparison expression

If a template has a numeric comparison such as

#if ($num > 7)

Does $num have to be an Integer or can it also be a Long or even a String ?

What about the case where $num did not come externaly from the application
Java code
but from the template itself as in

#set $num = 3

Will the numeric comparison still work ok ?

What about

#set $num = "3" ?

Thanks,

Tal


Re: Integers and numbers in a comparison expression

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Tal Dayan wrote:
> 
> > > The condition failed even though $num was higher than 7.
> > >
> > > Why not to throw an exception if the type of $num is invalid for
> > > the expression ?
> >
> > We could - we already log it.  An exception might be the way to go...
> 
> Oh, there is a log, I was not aware of it. Where is it found ? We are using
> the Velcoty app class and don't have any special configuration for the log,
> we
> are just configuring the the template loaders.

Oh, yes, there is a good log (so I think anyway).  The default log is
placed in the 'current directory' - if you are not paying attention in a
webapp, that will be the directory you start tomcat (or whatever) from
:)

See the servlet_example2 on how to ensure the velocity.log is placed in
your webapp.  Currently it goes into the root of your webapp (which
isn't so great...), but it still a good example..

And yes, teh log messages are rather complete and descriptive.

> 
> > > Also, without knowing much of the Velocity code, it looks to me that
> > > it would not be that difficult to convert a String value "134" to
> > > an integer when a integer value is required in the expression.
> > For example,
> > > one can write a generic method getIntegerValue(Object)->int that gets
> > > String, Long, Float, etc and converts then to an int. If the type
> > > of Object is invalid or the value does not represent an valid
> > numeric value
> > > than it can throw an exception. Am I missing something ?
> >
> > No - we've gone over this a few times. It is easy to do, but we wanted
> > to keep things simple.
> 
> Do you mean simple implementation or simple to the designer that
> use the template ? For the user, the ability to use strings in the
> expression will
> be simpler IMHO.
> 
> Tal

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

RE: Integers and numbers in a comparison expression

Posted by Tal Dayan <ta...@zapta.com>.
> > The condition failed even though $num was higher than 7.
> >
> > Why not to throw an exception if the type of $num is invalid for
> > the expression ?
>
> We could - we already log it.  An exception might be the way to go...

Oh, there is a log, I was not aware of it. Where is it found ? We are using
the Velcoty app class and don't have any special configuration for the log,
we
are just configuring the the template loaders.


> > Also, without knowing much of the Velocity code, it looks to me that
> > it would not be that difficult to convert a String value "134" to
> > an integer when a integer value is required in the expression.
> For example,
> > one can write a generic method getIntegerValue(Object)->int that gets
> > String, Long, Float, etc and converts then to an int. If the type
> > of Object is invalid or the value does not represent an valid
> numeric value
> > than it can throw an exception. Am I missing something ?
>
> No - we've gone over this a few times. It is easy to do, but we wanted
> to keep things simple.

Do you mean simple implementation or simple to the designer that
use the template ? For the user, the ability to use strings in the
expression will
be simpler IMHO.

Tal


Re: Integers and numbers in a comparison expression

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Tal Dayan wrote:
> 
> Thanks for the info. We figured it the hard way ;-)
> 
> We passed numeric values as a Long and
> 
>    num value ...... [$num]
> 
> worked well (since it has a toString() representation)
> but when we used it in a condition like (free syntax) :
> 
>   #if $num > 7
> 
> The condition failed even though $num was higher than 7.
> 
> Why not to throw an exception if the type of $num is invalid for
> the expression ?

We could - we already log it.  An exception might be the way to go...

> 
> Also, without knowing much of the Velocity code, it looks to me that
> it would not be that difficult to convert a String value "134" to
> an integer when a integer value is required in the expression. For example,
> one can write a generic method getIntegerValue(Object)->int that gets
> String, Long, Float, etc and converts then to an int. If the type
> of Object is invalid or the value does not represent an valid numeric value
> than it can throw an exception. Am I missing something ?

No - we've gone over this a few times. It is easy to do, but we wanted
to keep things simple.

This is one of the rough spots, certainly.  It will improve.

> 
> Tal
> 
> > -----Original Message-----
> > From: gmj@mta2.srv.hcvlny.cv.net [mailto:gmj@mta2.srv.hcvlny.cv.net]On
> > Behalf Of Geir Magnusson Jr.
> > Sent: Friday, July 13, 2001 7:00 PM
> > To: velocity-user@jakarta.apache.org
> > Subject: Re: Integers and numbers in a comparison expression
> >
> >
> > Tal Dayan wrote:
> > >
> > > If a template has a numeric comparison such as
> > >
> > > #if ($num > 7)
> > >
> > > Does $num have to be an Integer or can it also be a Long or
> > even a String ?
> >
> > Just an integer.  Working out a way to do Longs nicely, and forget about
> > strings... :)
> >
> > >
> > > What about the case where $num did not come externaly from the
> > application
> > > Java code
> > > but from the template itself as in
> > >
> > > #set $num = 3
> >
> > Ahem.
> >
> > #set($num = 3)
> >
> > > Will the numeric comparison still work ok ?
> >
> > Yes - that's an Integer.
> >
> > > What about
> > >
> > > #set $num = "3" ?
> >
> > #set( $num = "3")  or #set($num = '3')
> >
> > no, that's a string.
> >
> > > Thanks,
> > >
> > > Tal
> >
> > geir
> >
> > --
> > Geir Magnusson Jr.                           geirm@optonline.net
> > System and Software Consulting
> > Developing for the web?  See http://jakarta.apache.org/velocity/
> > You have a genius for suggesting things I've come a cropper with!
> >

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

RE: Integers and numbers in a comparison expression

Posted by Tal Dayan <ta...@zapta.com>.
Thanks for the info. We figured it the hard way ;-)

We passed numeric values as a Long and

   num value ...... [$num]

worked well (since it has a toString() representation)
but when we used it in a condition like (free syntax) :

  #if $num > 7

The condition failed even though $num was higher than 7.

Why not to throw an exception if the type of $num is invalid for
the expression ?

Also, without knowing much of the Velocity code, it looks to me that
it would not be that difficult to convert a String value "134" to
an integer when a integer value is required in the expression. For example,
one can write a generic method getIntegerValue(Object)->int that gets
String, Long, Float, etc and converts then to an int. If the type
of Object is invalid or the value does not represent an valid numeric value
than it can throw an exception. Am I missing something ?

Tal


> -----Original Message-----
> From: gmj@mta2.srv.hcvlny.cv.net [mailto:gmj@mta2.srv.hcvlny.cv.net]On
> Behalf Of Geir Magnusson Jr.
> Sent: Friday, July 13, 2001 7:00 PM
> To: velocity-user@jakarta.apache.org
> Subject: Re: Integers and numbers in a comparison expression
>
>
> Tal Dayan wrote:
> >
> > If a template has a numeric comparison such as
> >
> > #if ($num > 7)
> >
> > Does $num have to be an Integer or can it also be a Long or
> even a String ?
>
> Just an integer.  Working out a way to do Longs nicely, and forget about
> strings... :)
>
> >
> > What about the case where $num did not come externaly from the
> application
> > Java code
> > but from the template itself as in
> >
> > #set $num = 3
>
> Ahem.
>
> #set($num = 3)
>
> > Will the numeric comparison still work ok ?
>
> Yes - that's an Integer.
>
> > What about
> >
> > #set $num = "3" ?
>
> #set( $num = "3")  or #set($num = '3')
>
> no, that's a string.
>
> > Thanks,
> >
> > Tal
>
> geir
>
> --
> Geir Magnusson Jr.                           geirm@optonline.net
> System and Software Consulting
> Developing for the web?  See http://jakarta.apache.org/velocity/
> You have a genius for suggesting things I've come a cropper with!
>


Re: Integers and numbers in a comparison expression

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Tal Dayan wrote:
> 
> If a template has a numeric comparison such as
> 
> #if ($num > 7)
> 
> Does $num have to be an Integer or can it also be a Long or even a String ?

Just an integer.  Working out a way to do Longs nicely, and forget about
strings... :)

> 
> What about the case where $num did not come externaly from the application
> Java code
> but from the template itself as in
> 
> #set $num = 3

Ahem.

#set($num = 3)
 
> Will the numeric comparison still work ok ?

Yes - that's an Integer.
 
> What about
> 
> #set $num = "3" ?

#set( $num = "3")  or #set($num = '3')

no, that's a string.
 
> Thanks,
> 
> Tal

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!