0

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?

0__
  • 66,707
  • 21
  • 171
  • 266
Ivan
  • 63,011
  • 101
  • 250
  • 382

1 Answers1

2

Just do this:

implicit def stringFromFoo(foo: Foo): String = foo.toString
Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
  • And don't forget you'll need to import this method or place it somewhere compiler will find it (in superclass or a trait of your class, package object, etc.). – Alexey Romanov Oct 16 '11 at 05:57