在 Scala 中,“println(1,2)"是如何工作的? [英] In Scala, how come `println(1,2)` works?

查看:31
本文介绍了在 Scala 中,“println(1,2)"是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Scala (2.7.7final) 中,Predef.println 方法被定义为具有以下签名:

In Scala (2.7.7final), the Predef.println method is defined as having the following signature:

def println (x : Any) : Unit

怎么会这样:

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?

推荐答案

是的,如果没有合适的多参数方法和一个合适的单参数方法,编译器会尝试将逗号分隔的参数转换为元组.这不是隐式转换,只是编译器破解.这是一个有争议的功能,并且可能会在未来发生变化,因为计划围绕统一元组和参数列表的处理进行工作.

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.

这篇关于在 Scala 中,“println(1,2)"是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆