This page is no longer maintained — Please continue to the home page at www.scala-lang.org

pointer to implicit method

2 replies
richard emberson
Joined: 2010-03-22,
User offline. Last seen 42 years 45 weeks ago.

Can one get a pointer to an implicit method.

object Main {

trait Comp {
def name: String
}
class Leaf(_name: String) extends Comp {
def name: String = _name
}

trait Text {
def text: String
}
implicit def compToText(comp: Comp): Text =
new Text {
def text: String = "TEXT: " + comp.name
}

val l = new Leaf("ALeaf")
println(l.name)
println(l.text)

val nFn: (Comp) => () => String = _.name _
val iFn: (Comp) => () => String = _.text _

def main(args: Array[String]): Unit = {
}
}

make =>
Main.scala:23: error: type mismatch;
found : String
required: () => String
val iFn: (Comp) => () => String = _.text _
^
one error found

Thanks

d_m
Joined: 2010-11-11,
User offline. Last seen 35 weeks 2 days ago.
Re: pointer to implicit method

On Sat, Jan 28, 2012 at 11:43:29AM -0800, richard emberson wrote:
> Can one get a pointer to an implicit method.

I think you're getting tripped up by syntax.

Please see attached REPL session:

scala> implicit def xyz(i:Int) = new { def foo(j:Int) = i + j }
xyz: (i: Int)java.lang.Object{def foo(j: Int): Int}

scala> 3.foo(_)
res0: Int => Int =

scala> (3.foo _)
:9: error: missing arguments for method foo;
follow this method with `_' if you want to treat it as a partially applied function
(3.foo _)
^

scala> (3 foo _)
res2: Int => Int =

richard emberson
Joined: 2010-03-22,
User offline. Last seen 42 years 45 weeks ago.
Re: pointer to implicit method

The following does work:

val nFn: (Comp) => () => String = _.name _
//val iFn: (Comp) => () => String = _.text _
val iFn: (Comp) => String = _.text

A pointer to a normal method seems to have a different signature
than a pointer to an implicit method.

On 01/28/2012 12:52 PM, Erik Osheim wrote:
> On Sat, Jan 28, 2012 at 11:43:29AM -0800, richard emberson wrote:
>> Can one get a pointer to an implicit method.
>
> I think you're getting tripped up by syntax.
>
> Please see attached REPL session:
>
> scala> implicit def xyz(i:Int) = new { def foo(j:Int) = i + j }
> xyz: (i: Int)java.lang.Object{def foo(j: Int): Int}
>
> scala> 3.foo(_)
> res0: Int => Int =
>
> scala> (3.foo _)
> :9: error: missing arguments for method foo;
> follow this method with `_' if you want to treat it as a partially applied function
> (3.foo _)
> ^
>
> scala> (3 foo _)
> res2: Int => Int =
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland