Scala implicit conversion of apply method

Viewed 1128

I tried the following to create an option-checking style in code:

object Test {
  trait Check
  object x extends Check
  def option() = false
  def option(xx: Check) = true
  implicit class AugmentCheck(s: String) {
    def apply() = ""
    def apply(xx: Check) = s
  }
}
import Test._

val delete = option ( x )
val force  = option (   )

val res = "mystring" (   )

But I face the following problem:

<console>:11: error: type mismatch;
 found   : String("mystring")
 required: ?{def apply: ?}
Note that implicit conversions are not applicable because they are ambiguous:
 both method augmentString in object Predef of type (x: String)scala.collection.
immutable.StringOps
 and method AugmentCheck in object Test of type (s: String)Test.AugmentCheck
 are possible conversion functions from String("mystring") to ?{def apply: ?}
           val res = "mystring" (   )
                     ^
<console>:11: error: String("mystring") does not take parameters
           val res = "mystring" (   )

This is unfortunate, because even if there is ambiguity in the symbol name, there shouldn't be any ambiguity in the function signature.

If I introduce the method "apply", it works fine when there is an argument, but not without:

           val res = "mystring" apply ( x )

           res == ""

How can I remove the keyword "apply" in this case?

2 Answers
Related