Given the following trait and companion object definitions:
trait Api {
def foo(s: String): String
}
object Api extends Api {
override def foo(s: String) = s
}
I would like to extend the companion object's foo method to have a property function and for that, I use the following implicit class:
implicit class ExtendApi(api: Api.type) {
object foo {
def asInt(s: String): Int = s.toInt
}
}
println (Api.foo.asInt("1")) // Does not work
However, using this does compile. I get an error:
missing argument list for method foo in object Api
Unapplied methods are only converted to functions when a function type is expected.
You can make this conversion explicit by writing `foo _` or `foo(_)` instead of `foo`.
It seems scala is not resolving the implicit definition to find the foo
object.
I tried to be explicit by writing:
println (implicitly[Api.type].foo.asInt("1"))
Now I get another error:
could not find implicit value for parameter e: Playground.this.Api.type
This new error does not make sense to me.
Is what I am trying to do possible? How can I fix the errors? Thank you.