问题
In Scala (2.7.7final), the Predef.println
method is defined as having the following signature:
def println (x : Any) : Unit
How come, then that the following works:
scala> println(1,2)
(1,2)
Does the compiler automatically convert a comma-separated list of arguments into a Tuple? By what magic? Is there an implicit conversion going on here, and if so, which one?
回答1:
Yes, the compiler will attempt to convert comma separated arguments into tuples, if there are no appropriate multi-argument methods and a single appropriate one-argument method. It's not an implicit conversion, just a compiler hack. This is a somewhat controversial feature, and will probably undergo changes going forward, as work is planned around unifying the treatment of tuples and argument lists.
来源:https://stackoverflow.com/questions/3526156/in-scala-how-come-println1-2-works