- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Number[A] trait?
Hello,
In Haskell one can define a simple function like this:
Prelude> let add1 x = x + 1
Prelude> add1 2
3
Prelude> add1 2.0
3.0
Prelude> :type add1
add1 :: (Num a) => a -> a
Would it be possible to do the same thing in Scala? I mean:
def add1[U <% Number[U]](x:U) = x + 1
I made several attempt to define a trait Number[A] like in Ordered[A] but all of them failed. I guess if it was easy it would already be there...
Thanks,
Sebastien
In Haskell one can define a simple function like this:
Prelude> let add1 x = x + 1
Prelude> add1 2
3
Prelude> add1 2.0
3.0
Prelude> :type add1
add1 :: (Num a) => a -> a
Would it be possible to do the same thing in Scala? I mean:
def add1[U <% Number[U]](x:U) = x + 1
I made several attempt to define a trait Number[A] like in Ordered[A] but all of them failed. I guess if it was easy it would already be there...
Thanks,
Sebastien










Re: Number[A] trait?
Something like
def add1[T](v: { def +(T):T }):T = v + 1
(Not entirely sure about the syntax...
BR,John
On Fri, Jan 9, 2009 at 12:26 AM, Sebastien Bocq <sebastien [dot] bocq [at] gmail [dot] com> wrote:
Re: Number[A] trait?
In theory, yes. But it doesn't work in Scala.
Ricky.
2009/1/9 John Nilsson <john [at] milsson [dot] nu>
Re: Number[A] trait?
Sebastien Bocq wrote:
> a trait Number[A] like in Ordered[A]
http://lampsvn.epfl.ch/svn-repos/scala/scala/branches/devel-base-2.8.0/s...
The code appears in the recent scala-2.8.0... nightly builds from
http://www.scala-lang.org/node/212 : download and install, then expand
src/scala-library-src.jar and see scalax/Numeric.scala
Re: Re: Number[A] trait?
We all know that equality is a tricky issue
(cfr. discussion in one of the chapters of the Scala Book)
... just out of interest ...
I guess that defining equality in terms of a trait Equal[A]
has never been an option because of Java compatibility issues?
Or am I plain wrong here?
Luc
On Fri, Jan 9, 2009 at 10:37 AM, Eric Willigers <ewilligers [at] gmail [dot] com> wrote:
--
__~O
-\ <,
(*)/ (*)
reality goes far beyond imagination
Re: Re: Number[A] trait?
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi Luc,
I prefer the flexibility of such a trait:
http://scalaz.googlecode.com/svn/artifacts/latest/scaladoc/scalaz/Equal....
Then I use === (that's 3 =) and /= methods for comparing for equality:
http://scalaz.googlecode.com/svn/artifacts/latest/scaladoc/scalaz/EqualW...
Luc Duponcheel wrote:
> Along the same lines:
>
> We all know that equality is a tricky issue
> (cfr. discussion in one of the chapters of the Scala Book)
>
> ... just out of interest ...
>
> I guess that defining equality in terms of a trait Equal[A]
> has never been an option because of Java compatibility issues?
>
> Or am I plain wrong here?
>
> Luc
>
> On Fri, Jan 9, 2009 at 10:37 AM, Eric Willigers
> > wrote:
>
> Sebastien Bocq wrote:
>
> a trait Number[A] like in Ordered[A]
>
>
>
http://lampsvn.epfl.ch/svn-repos/scala/scala/branches/devel-base-2.8.0/s...
>
> The code appears in the recent scala-2.8.0... nightly builds
> from http://www.scala-lang.org/node/212 : download and install,
> then expand src/scala-library-src.jar and see scalax/Numeric.scala
>
>
>
>
> --
> __~O
> -\ <,
> (*)/ (*)
>
> reality goes far beyond imagination
>
- --
Tony Morris
http://tmorris.net/
S, K and I ought to be enough for anybody.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iEYEARECAAYFAklnM3kACgkQmnpgrYe6r63n1ACfdhZexlxixwk6Appfx9RKWSPx
Tj4AoMWLbhjYebFFvUQl5IScFNUpAsSj
=8dqs
-----END PGP SIGNATURE-----
Re: Re: Number[A] trait?
On Fri, Jan 9, 2009 at 6:22 AM, Tony Morris <tmorris [at] tmorris [dot] net> wrote:
Re: Re: Number[A] trait?
The remainder left as an exercise.
2009/1/9 Josh Suereth <joshua [dot] suereth [at] gmail [dot] com>
Re: Number[A] trait?
http://paste.pocoo.org/show/97661/
And a bit of context: http://rickyclarkson.blogspot.com/2009/01/typeclass-pattern.html
2009/1/8 Sebastien Bocq <sebastien [dot] bocq [at] gmail [dot] com>
Re: Number[A] trait?
On Thu, Jan 08, 2009 at 11:53:48PM +0000, Ricky Clarkson said
> I blogged about something related to this recently, and while my blog
> doesn't answer your question, James Iry provided this code in a comment that
> does:
>
> http://paste.pocoo.org/show/97661/
>
> And a bit of context:
> http://rickyclarkson.blogspot.com/2009/01/typeclass-pattern.html
>
This still does not allow you to write generic sections. I'm not
entirely sure but I think this is because in haskell numeric constants
are polymorphic:
Prelude> :t 1
1 :: (Num t) => t
while in scala they are monomorphic:
scala> 1
res0: Int = 1
This means in haskell the operation will drive the type of the constant
but in scala the constant will drive the type of the operation.
Re: Number[A] trait?
2009/1/9 Geoff Reedy <geoff [at] programmer-monk [dot] net>
Re: Number[A] trait?
2009/1/9 Ricky Clarkson <ricky [dot] clarkson [at] gmail [dot] com>