sourcecode icon indicating copy to clipboard operation
sourcecode copied to clipboard

Compatibility with implicit resolution

Open acjay opened this issue 8 years ago • 1 comments

I'm trying to capture the literal floating-point number that a person used in a data structure:

scala> trait Capture { def text: String }
defined trait Capture

scala> implicit class DoubleCapture(source: sourcecode.Text[Double]) extends Capture { def text = source.source }
defined class DoubleCapture
                    ^

scala> val x: Capture = 5.0
<console>:49: error: type mismatch;
 found   : Double(5.0)
 required: Capture
       val x: Capture = 5.0

If DoubleCapture takes a Double, this works fine. But I don't want the Double itself, what I really want is the string representation from the code itself. I was hoping I could use sourcecode.Text to do this, but it doesn't seem to work.

acjay avatar May 09 '17 23:05 acjay

Implicit conversions are, unfortunately, not chainable. I don't know of any way around that in this case. The normal solution of

implicit class DoubleCapture[T](source: T)(implicit f: T => sourcecode.Text[Double])

Doesn't work in this case, because we need the sourcecode.Text(...) wrapped to lexically wrap the expression that we are being given.

If anyone has any ideas I'd love to hear them, but otherwise I don't know if/how/when we can fix this

lihaoyi avatar Aug 11 '17 08:08 lihaoyi