I have the following code to print the numbers from 1 to 9 in letters
class IntToNumber(num:Int) {
val digits = Map("1" -> "one", "2" -> "two", "3" -> "three", "4" -> "four", "5" -> "five", "6" -> "six", "7" -> "seven", "8" -> "eight", "9" -> "nine")
def inLetters():String = {
digits.getOrElse(num.toString,"")
}
}
implicit def intWrapper(num:Int) = new IntToNumber(num)
(1 until 10).foreach(n => println(n.inLetters))
When I run this code I get an error saying the method is not available for Long
Script.scala:9: error: value inLetters is not a member of Long
(1 until 10).foreach(n => println(n.inLetters))
^
one error found
Changing the last line to
(1 until 10).foreach(n => println(n.toInt.inLetters))
Works fine..
Can someone help me understand Why is that (1 until 10) range returning Long and not int?
I’ve changed the name of your implicit conversion to
intWrapperX. The following session shows the fixed example.The problem is, that your
intWrappershadowsscala.Predef.intWrapper(i:Int): RichIntwhich is needed to create theRangeobject. I leave the explanation of why the conversion toLong(or presumableRichLong) kicks in to the commenters.