Write an implicit function which takes a string as a parameter and converts it to a CustomString. Write another implicit function which takes a string and converts it to CustomInt.
i done the following code:
import scala.language.implicitConversions
object ImplicitConversion extends App {
class CustomString(val s: String) {
def findLength = s.length
}
class CustomInt(val number : Int){
def multiply = number*5
}
implicit def StrToCustomString(s:String):CustomString = new CustomString(s)
implicit def StrToCustomInt(output2:String):CustomString = new CustomString(s)
val output1 = “100”.findLength
val output2 = “100”.multiply
println(output1, output2)
}
import scala.language.implicitConversions
object ImplicitConversion extends App {
class CustomString(val s: String) {
def findLength = s.length
}
class CustomInt(val number : Int){
def multiply = number*5
}
implicit def StrToCustomString(s:String):CustomString = new CustomString(s)
implicit def StrToCustomInt(output2:String):CustomInt = new CustomInt(s)
val output1 = “100”.findLength
val output2 = “100”.multiply
println(output1, output2)
}
i am getting the following error:
$ scalac ImplicitConversion.scala
ImplicitConversion.scala:13: error: illegal character ‘\u201c’
val output1 = “100”.findLength
^
ImplicitConversion.scala:13: error: illegal character ‘\u201d’
val output1 = “100”.findLength
^
ImplicitConversion.scala:14: error: illegal character ‘\u201c’
val output2 = “100”.multiply
^
ImplicitConversion.scala:14: error: illegal character ‘\u201d’
val output2 = “100”.multiply
^
four errors found
I have corrected the double quote , but now i am getting following error:
$ scalac ImplicitConversion.scala
ImplicitConversion.scala:12: error: not found: value s
implicit def StrToCustomInt(output2:String):CustomInt = new CustomInt(s)
^
one error found
That error message is obvious - you called your argument output2 but tried to use s in the function definition. But you will get another error when you correct this, which you solve using .toInt for strings (if you can be unsafe, otherwise you should test if s is an integer).