Problem converting numbers to JsExp

67 views
Skip to first unread message

Jeppe Nejsum Madsen

unread,
Jan 31, 2011, 10:06:01 AM1/31/11
to lif...@googlegroups.com
Hi

I was updating our app to latest 2.3-SNAPSHOT and saw that

numToJsExp was removed. I guess the correct way to now convert a
number to JsExp is to use Num(x)??

But this doesn't play so nice with Scala's type system afaics:

scala> case class Num(n:Number)
defined class Num

scala> def f:Option[Double] = None
f: Option[Double]

scala> Num(f.getOrElse(0.0))
<console>:9: error: type mismatch;
found : Any
required: java.lang.Number
Num(f.getOrElse(0.0))
^

Of course I can cast the value to Double, but this gets ugly fast. I
believe this could be solved by redefining Num:

case class Num2[T:Numeric](n:T)

scala> Num2(f.getOrElse(0.0))
res22: Num2[Double] = Num2(0.0)

I'm I missing something or should we make this change?

/Jeppe

David Pollak

unread,
Jan 31, 2011, 2:39:13 PM1/31/11
to lif...@googlegroups.com
Open a ticket and I'll get right on it.


--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Blog: http://goodstuff.im
Surf the harmonics

David Pollak

unread,
Jan 31, 2011, 3:49:51 PM1/31/11
to lif...@googlegroups.com
On Mon, Jan 31, 2011 at 7:06 AM, Jeppe Nejsum Madsen <je...@ingolfs.dk> wrote:
Hi

I was updating our app to latest 2.3-SNAPSHOT and saw that

numToJsExp was removed. I guess the correct way to now convert a
number to JsExp is to use Num(x)??

Actually, the implicits were moved from JE to the JsExp companion object.
 

But this doesn't play so nice with Scala's type system afaics:

scala> case class Num(n:Number)
defined class Num

scala> def f:Option[Double] = None
f: Option[Double]

scala> Num(f.getOrElse(0.0))
<console>:9: error: type mismatch;
 found   : Any

Sometime type inference is broken.  This is one of those cases.

I've added explicit constructors for Int, Float, Double, and Long which should address the problem.
 
 required: java.lang.Number
      Num(f.getOrElse(0.0))
                     ^

Of course I can cast the value to Double, but this gets ugly fast. I
believe this could be solved by redefining Num:

case class Num2[T:Numeric](n:T)

scala> Num2(f.getOrElse(0.0))
res22: Num2[Double] = Num2(0.0)

I'm I missing something or should we make this change?

/Jeppe
--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.

Jeppe Nejsum Madsen

unread,
Feb 1, 2011, 5:20:07 AM2/1/11
to lif...@googlegroups.com
David Pollak <feeder.of...@gmail.com> writes:

> On Mon, Jan 31, 2011 at 7:06 AM, Jeppe Nejsum Madsen <je...@ingolfs.dk>wrote:
>
>> Hi
>>
>> I was updating our app to latest 2.3-SNAPSHOT and saw that
>>
>> numToJsExp was removed. I guess the correct way to now convert a
>> number to JsExp is to use Num(x)??
>>
>
> Actually, the implicits were moved from JE to the JsExp companion object.

Ok, just saw them commented out in JE.

>
>
>>
>> But this doesn't play so nice with Scala's type system afaics:
>>
>> scala> case class Num(n:Number)
>> defined class Num
>>
>> scala> def f:Option[Double] = None
>> f: Option[Double]
>>
>> scala> Num(f.getOrElse(0.0))
>> <console>:9: error: type mismatch;
>> found : Any
>>
>
> Sometime type inference is broken. This is one of those cases.
>
> I've added explicit constructors for Int, Float, Double, and Long which
> should address the problem.

Due to the broken type inference, this still needs two type annotations
(plus two more because the List constructor is not picked up) :

JsArray(dataset.series.map(s => JsArray(categories.map {cat => (s.valueOf(cat).getOrElse(0.0):Double):JsExp} : _*)) : _*)

The auxilary constructor taking a List on JsArray is not copied to the
companion object (see here http://www.scala-lang.org/node/976) so I
suggest we create a JsArray companion object?

I still think the change to Num I proposed works better with type
inference, at least in this example. Are there any drawbacks?

/Jeppe

David Pollak

unread,
Feb 1, 2011, 11:56:41 AM2/1/11
to lif...@googlegroups.com

Open a ticket and we'll get it into 2.3-RC1
 

/Jeppe

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.

Jeppe Nejsum Madsen

unread,
Feb 1, 2011, 3:11:25 PM2/1/11
to lif...@googlegroups.com

lester

unread,
Mar 16, 2011, 12:48:08 PM3/16/11
to Lift
Hello,

As I see, JsExp is no longer subclass of scala.xml.SpecialNode in 2.3-
M1. As far as I understand this was done as part of issue #881 -
Improve Js conversion: https://www.assembla.com/spaces/liftweb/tickets/881-improve-js-conversion
. Am I right?

Thank you.


On 1 фев, 23:11, Jeppe Nejsum Madsen <je...@ingolfs.dk> wrote:
> On Tue, Feb 1, 2011 at 5:56 PM, David Pollak
>
>
>
>
>
>
>
>
>
> <feeder.of.the.be...@gmail.com> wrote:
>
> > On Tue, Feb 1, 2011 at 2:20 AM, Jeppe Nejsum Madsen <je...@ingolfs.dk>
> > wrote:
>
> >> companion object (see herehttp://www.scala-lang.org/node/976) so I
> >> suggest we create a JsArray companion object?
>
> >> I still think the change to Num I proposed works better with type
> >> inference, at least in this example. Are there any drawbacks?
>
> > Open a ticket and we'll get it into 2.3-RC1
>
> Done:https://www.assembla.com/spaces/liftweb/tickets/881-improve-js-conver...
>
> /Jeppe

Jeppe Nejsum Madsen

unread,
Mar 16, 2011, 4:50:21 PM3/16/11
to lif...@googlegroups.com
On Wed, Mar 16, 2011 at 5:48 PM, lester <leste...@gmail.com> wrote:
> Hello,
>
> As I see, JsExp is no longer subclass of scala.xml.SpecialNode in 2.3-
> M1. As far as I understand this was done as part of issue #881 -
> Improve Js conversion: https://www.assembla.com/spaces/liftweb/tickets/881-improve-js-conversion
> . Am I right?

No :-) If you look at the comments to that ticket (and the commit)
you'll see that only a JsArray constructor was added.

/Jeppe

David Pollak

unread,
Mar 16, 2011, 5:01:37 PM3/16/11
to lif...@googlegroups.com
On Wed, Mar 16, 2011 at 4:48 PM, lester <leste...@gmail.com> wrote:
Hello,

As I see, JsExp is no longer subclass of scala.xml.SpecialNode in 2.3-
M1. As far as I understand this was done as part of issue #881 -
Improve Js conversion: https://www.assembla.com/spaces/liftweb/tickets/881-improve-js-conversion
. Am I right?

The fact that JsExp was a subclass of SpecialNode caused no end of problems and solved very few, so I removed the dependency.
 
>
> /Jeppe

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.




--
Lift, the simply functional web framework http://liftweb.net

lester

unread,
Mar 16, 2011, 6:23:29 PM3/16/11
to Lift
Jeppe, David,

Understood. Thanks for reply!

On 17 мар, 00:01, David Pollak <feeder.of.the.be...@gmail.com> wrote:
> On Wed, Mar 16, 2011 at 4:48 PM, lester <lester.m...@gmail.com> wrote:
> > Hello,
>
> > As I see, JsExp is no longer subclass of scala.xml.SpecialNode in 2.3-
> > M1. As far as I understand this was done as part of issue #881 -
> > Improve Js conversion:
> >https://www.assembla.com/spaces/liftweb/tickets/881-improve-js-conver...
> Beginning Scalahttp://www.apress.com/book/view/1430219890
Reply all
Reply to author
Forward
0 new messages