Ryan
Ryan

Reputation: 14659

Implicit conversions in scala

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

Upvotes: 0

Views: 1904

Answers (3)

Pavel
Pavel

Reputation: 1539

Here is a typical approach, yes, Implicit's very similar to C# static methods and its implemented in adhoc way:

object Foo {
  def print(message: String) = println(message)
}

class Bar {
  val baz = 1
}

object BarToStringMaker {
   implicit def barToString(b : Bar) : String = b.baz.toString
}

import BarToStringMaker._

Foo.print(new Bar)

more reading:

http://www.artima.com/pins1ed/implicit-conversions-and-parameters.html

hope that helps,

Upvotes: 1

Kolmar
Kolmar

Reputation: 14224

You can put implicit conversions that can be applied automatically without importing into the companion object of the class:

class Bar {
  val baz = 1
}

// This should be the companion object of `Bar`, so if in console, use :paste mode
object Bar {
  implicit def toString(bar: Bar): String = bar.toString
}

scala> Foo.print(new Bar) // works without `import Bar._`
$line7.$read$$iw$$iw$Bar@280ecc33

For Enumerations you can put conversions into the object itself:

object Color extends Enumeration {

  type Method = Value

  val RED, BLUE, GREEN = Value

  implicit def toString(value: Value): String = value.toString 
}

scala> ColorPrinter.print(Color.RED) // works without `import Color._`
RED

You can read more about implicit resolution in Scala in this answer: https://stackoverflow.com/a/5598107/1098230

Upvotes: 2

Dima
Dima

Reputation: 40510

Converting things implicitly to basic types, like String or Int isn't really a very good idea (the implicit conversion may trigger in places where you do not expect it to, creating subtle, hard to debug, issues).

Why not just make it explicit? ?

class Bar {
   val baz = 1
   def print = Foo.print(toString)
}

new Bar().print

Upvotes: 2

Related Questions