I'm going to steal MissingFaktor's answer, but change it a bit.
case class Rational(num: Int, den: Int) { def /(d2: Int) = Rational(num, den * d2) } object Rational { implicit def rationalWhole(num: Int) = new { def r = Rational(num, 1) } }
Then you can do something like this, which, I think, is a little better than using a backslash, and more consistent, since you want to define all the usual numerical operators on Rational anyway:
scala> 1.r / 2 res0: Rational = Rational(1,2)
Kris nuttycombe
source share