- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
pointer to implicit method
Sat, 2012-01-28, 20:43
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
Sat, 2012-01-28, 22:21
#2
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 =
>
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 =