Does Scala have implicit conversions for objects? For example, if I have a method with the following signature:
object Foo {
def print(message: String) = println(message)
}
class Bar {
val baz = 1
}
How can I call Foo.print(new Bar)
?
Can I put a method on the Bar
class to implicitly convert Bar
instance to a string without having to call toString
in the argument?
C# has this and I'm wondering if scala does too.
Let's say we have Scala enum:
object Color extends Enumeration {
type Method = Value
val RED, BLUE, GREEN = Value
}
Then I have a class:
object ColorPrinter {
def print(x: String) = {
println(x)
}
}
ColorPrinter's print method can't be changed.
I want to call into ColorPrinter.print(Color.RED)
, but I can't do that. I would have to call it like this: ColorPrinter.print(Color.RED.toString)
.
I want to avoid having to toString