In Scala, how come `println(1,2)` works?

↘锁芯ラ 提交于 2019-12-01 14:58:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!