No implicit conversion from Int to Number

23 views
Skip to first unread message

Lionel Parreaux

unread,
Nov 24, 2014, 1:26:25 PM11/24/14
to spire...@googlegroups.com
Hello,

I just started playing with Spire and it seems great.
However I came up with what looks like a strange case of omission: is there a good reason why there is no implicit conversion from Int to Number? (there is one for Long, etc.).
This creates a problem with the following code on Scala 2.10:

    import spire.algebra._
    import spire.math._
    import spire.implicits._
   
    case class V[N](x: N, y: N)
   
    object V {
      implicit def tuple2V[N, M <% N](t: (M,M)) = V[N](t._1, t._2)
    }
   
    object Test {
     
    //  implicit def int2Num(n: Int) = Number(n)
   
      val v: V[Number] = (0L,0L)
     
    }

Without the implicit def I commented, it does not compiler -- it does when using (0L,0L).

Should I be doing it differently, or is it an oversight from Spire devs?

Thanks,
LP.

Erik Osheim

unread,
Nov 24, 2014, 1:49:04 PM11/24/14
to Lionel Parreaux, spire...@googlegroups.com
Hi Lionel,

So, I think current best practice in Scala is to avoid using <%.
(A <% B) literally asks "is there an implicit conversion from M to N?"
which is a very specific question.

Right now the implicit from Long works for most cases, but as you've
seen it definitely won't work for this. I'm open to considering it,
but I would rather not be forced to add implicits from every possible
number type (e.g. Byte, Short, etc) just to support <%. Right now we
mainly support these kinds of implicit conversions to make working
with literals easier (e.g. x + 1).

Can you give an idea of what you're trying to accomplish here? Is your
V type intended to be a two-dimensional vector? A point? It's possible
that a different design is possible, e.g.

implicit class VOps[M](t: (M, M)) {
def toV(implicit c: ConvertableFrom[M]): V[Number] =
V(c.toNumber(t._1), c.toNumber(t._2))
}

Anyway, hopefully we can figure out something that works. I'm worried
that if we start trying to support every possible use of <% we will be
completely overwhelmed.

-- Erik

Tom Switzer

unread,
Nov 24, 2014, 1:50:23 PM11/24/14
to Erik Osheim, Lionel Parreaux, Spire User List
We already support implicit conversions from most other types. For a number type like Number, I think this is somewhat reasonable, since it is meant to Just Work. I agree, for more specialized types, relying on implicits probably isn't the right way.

Erik Osheim

unread,
Nov 24, 2014, 2:05:21 PM11/24/14
to Tom Switzer, Lionel Parreaux, Spire User List
Alright, I think this is a good principle for Number.

So yes, it was definitely an oversight! Sorry for my defensive reply,
I think the use of <% sort of freaked me out a little bit.

Tom has opened an issue, so this will get fixed soon (and will be in
the next release).

-- Erik

Lionel Parreaux

unread,
Nov 24, 2014, 4:47:53 PM11/24/14
to Erik Osheim, Tom Switzer, Spire User List
Thanks for the answers!

Sorry if it looked like I was saying Spire should support all uses of <%, it's just that it looked weird that there were conversions for most types but not Int.

As for my problem, I only came up with <% because I thought it was very nice to be able to write just (a,b) instead of Vector[Number](a,b) like in:
  Rectangle(pos, pos + (25,30))

It used to work without <% for usual numeric types, before I switched to Spire and tried using the Number type. Admittedly, it's probably not best to try to maintain this nice syntax using view bounds. I'm still unsure to what extent implicits should be used in Scala... It's often difficult to find the sweet spot between nice syntax, flexibility and the amount of headache involved for making the system work :)

Cheers,
LP.


Reply all
Reply to author
Forward
0 new messages