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

查看:71
本文介绍了在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

为什么会出现以下情况?

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?

推荐答案

是的,如果没有合适的多参数方法和一个合适的单参数方法,编译器将尝试将逗号分隔的参数转换为元组.这不是隐式转换,只是编译器hack.这是一个有争议的功能,随着计划围绕统一元组和参数列表的处理而开展的工作,它可能会发生变化.

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天全站免登陆