Mark is a graph advocate and field engineer for Neo Technology, the company behind the Neo4j graph database. As a field engineer, Mark helps customers embrace graph data and Neo4j building sophisticated solutions to challenging data problems. When he's not with customers Mark is a developer on Neo4j and writes his experiences of being a graphista on a popular blog at http://markhneedham.com/blog. He tweets at @markhneedham. Mark is a DZone MVB and is not an employee of DZone and has posted 516 posts at DZone. You can read more from them at their website. View Full User Profile

Scala: Which implicit conversion is being used?

11.09.2011
| 2815 views |
  • submit to reddit

Last week my colleague Pat created a method which had a parameter which he wanted to make optional so that consumers of the API wouldn’t have to provide it if they didn’t want to.

We ended up making the method take in an implicit value such that the method signature looked a bit like this:

def foo[T](implicit blah:(String => T)) = {
  println(blah("mark"))
  "foo"
}

We can call foo with or without an argument:

scala> foo { x => x + " Needham" } 
mark Needham
res16: java.lang.String = foo
scala> foo 
mark
res17: java.lang.String = foo

In the second case it seems like the function is defaulting to an identity function of some sorts since the same value we pass to it is getting printed out.

We figured that it was probably using one of the implicit conversions in Predef but weren’t sure which one.

I asked about this on the Scala IRC channel and Heikki Vesalainen suggested running scala with the ‘-print’ flag to work it out.

scala -print

The output is pretty verbose but having defined foo as above this is some of the output we get when calling it:

scala> foo
[[syntax trees at end of cleanup]]// Scala source: <console>
package $line2 {
  final object $read extends java.lang.Object with ScalaObject {
    def this(): object $line2.$read = {
      $read.super.this();
      ()
    }
  };
  final object $read$$iw$$iw extends java.lang.Object with ScalaObject {
    private[this] val res0: java.lang.String = _;
    <stable> <accessor> def res0(): java.lang.String = $read$$iw$$iw.this.res0;
    def this(): object $line2.$read$$iw$$iw = {
      $read$$iw$$iw.super.this();
      $read$$iw$$iw.this.res0 = $line1.$read$$iw$$iw.foo(<strong>scala.this.Predef.conforms()</strong>);
      ()
    }
  };
  final object $read$$iw extends java.lang.Object with ScalaObject {
    def this(): object $line2.$read$$iw = {
      $read$$iw.super.this();
      ()
    }
  }
}

I’ve highlighted the call to Predef.conforms() which is the implicit conversion that’s been substituted into ‘foo’.

It’s defined like so:

Predef.scala

implicit def conforms[A]: A <:< A = new (A <:< A) { def apply(x: A) = x }

I’m not sure where that would be legitimately used but the comments just above it suggest the following:

An instance of `A <:< B` witnesses that `A` is a subtype of `B`.

This is probably a misuse of implcits and we intend to replace the implicit in our code with a default function value but it was interesting investigating where the implicit had come from!

 

From http://www.markhneedham.com/blog/2011/11/06/scala-which-implicit-conversion-is-being-used/

Published at DZone with permission of Mark Needham, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)