问题
Is there a manner to import a specific method signature?
def test() {
lazy val log = LoggerFactory.getLogger("AndroidProxy")
import log.{error, debug, info, trace}
trace("This is a test")
trace "This is also" // <- This line will not compile
}
Perhaps it's not possible, but my primary goal is to allow this without adding in a new method. I've tried these to no avail
import log.{error => error(_:String)}
import log.{error(x: String) => error(x)}
I suppose the primary challenge is that all of the methods take one argument. I can call no-argument methods without (), and I can create a chain of method calls that omits the .'s e.g. foo getX toString
, but I don't know how to create an arity-1 call automatically
This is a followup to this question.
回答1:
The issue with the code:
trace "This is also" // <- This line will not compile
is not that you're somehow importing too many overloaded variants of trace
- it's that you can't use Scala's infix notation this way. An expression like:
e op
is interpreted as a "Postfix Operation" (see section 6.12.2 of the Scala Language Specification), equivalent to the call:
e.op
So your code would be equivalent to:
trace."This is also"
which is of course a compile error.
If you instead use an "Infix Operation" of the form e1 op e2
(section 6.12.3 of the Scala Language Specification), then there aren't any problems even if the method is overloaded:
scala> class Logger { def trace(s: String) = "1arg"; def trace(i: Int, s: String) = "2arg" }
defined class Logger
scala> val log = new Logger
log: Logger = Logger@63ecceb3
scala> log trace "This is also"
res0: String = 1arg
回答2:
No, there is not a way to import a specific method signature.
来源:https://stackoverflow.com/questions/14906830/import-specific-method-signature-in-scala