Rob N
Rob N

Reputation: 16429

How to use implicitly to find a conversion to a type with a method name?

In another answer here they give the following code example:

scala> implicitly[Int => { def min(i: Int): Any }]
res22: (Int) => AnyRef{def min(i: Int): Any} = <function1>

That doesn't work in my scala console (2.10.0-RC2).

scala> implicitly[Int => { def min(i: Int): Any }]
<console>:8: error: No implicit view available from Int => AnyRef{def min(i: Int): Any}.
          implicitly[Int => { def min(i: Int): Any }]
                    ^
scala> 12 min 11
res15: Int = 11

What is the new way to do it? And what does that syntax mean anyway? I'm not familiar with it -- specifically the part { def min(i: Int): Any }, used as a type expression. Is that defining an anonymous type of some kind?

I want to do this because I'd like to track down an implicit conversion when I see it in code and have no idea where it is imported from. For example, the other day I saw some code that was calling format on a java.util.Date, which doesn't have format. I didn't know which import pulled in the conversion.

Upvotes: 2

Views: 166

Answers (1)

Rex Kerr
Rex Kerr

Reputation: 167901

You're not going to find min because RichInt is a value class now (which doesn't work with structural typing--that has to be an AnyRef).

But the strategy will work otherwise:

scala> implicitly[Option[Int] => { def iterator: Iterator[Int] }]
res29: Option[Int] => AnyRef{def iterator: Iterator[Int]} = <function1>

So the same trick will work, just not with value classes. Try an IDE instead.

Upvotes: 5

Related Questions