How to define an impicit conversion from one to another existing type in Scala?
-
10-02-2021 - |
Domanda
Let's say we've got a Foo
class in a library we use and want (in a code file of ours) Foo
instances to be implicitly cast to String
instances whenever a Foo
instance is met in a place a String
instance is required. How to achieve this in Scala?
Soluzione
Just do this:
implicit def stringFromFoo(foo: Foo): String = foo.toString
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow