0

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.

Community
  • 1
  • 1
Hamy
  • 20,662
  • 15
  • 74
  • 102
  • 1
    That line will not compile because it is plain illegal. The error message is just an artifact of the compiler trying to make sense of it, I expect. Remove the line above, while preserving the empty line between the import and it, and see what it shows. – Daniel C. Sobral Feb 16 '13 at 04:22
  • Yup, it's a completely different error. I'll update the question – Hamy Feb 16 '13 at 04:39

2 Answers2

3

No, there is not a way to import a specific method signature.

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
  • 3
    To elaborate... Importing is about names alone. So no, you cannot import only a particular signature, only the entire overload set for a given name. – Randall Schulz Feb 16 '13 at 05:36
3

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
Steve
  • 3,038
  • 2
  • 27
  • 46