为什么以及如何在调用单参数函数时特别处理元组? [英] Why and how is Scala treating a tuple specially when calling a one arg function?

查看:16
本文介绍了为什么以及如何在调用单参数函数时特别处理元组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

scala 将多个函数调用参数合并到一个元组中——这可以禁用吗? 讨论了 Scala 创建一个元组以绑定到一个元组arg 函数.这导致

scala coalesces multiple function call parameters into a Tuple -- can this be disabled? discusses Scala creating a tuple to bind to one arg function. This results in

scala> println(1, 2)
(1,2)

答案是编译器允许在没有括号的情况下调用一个 arg 函数,因此从逻辑上讲,这是对带有元组的 println 的调用.

The answer says that the compiler allows one arg functions to be called with no parens, so that logically this is a call to println with a tuple.

但是 println 不能用单个元组参数调用

But println cannot be called with a single tuple parameter

scala> val t = (1, 2)
t: (Int, Int) = (1,2)

scala> println t
<console>:6: error: value t is not a member of Unit
       println t
           ^

所以其他事情正在发生.为什么元组在这里很特别?

so something else is going on. Why are tuples special here?

推荐答案

这个解释,Scala parses println(1,2)(或Console println (1,2) 与解析任何两个参数的方法调用的方式相同.稍后,编译器通过将方法参数包装在一个元组中来转换调用,以匹配实际的方法类型签名.

Contrary to this explanation, Scala parses println(1,2) (or Console println (1,2) for that matter) the same way it parses any two-argument method call. Later, the compiler transforms the call by wrapping the method arguments in a tuple to match the actual method type signature.

如果编译器不这样做,像 Console println (1,2) 这样完全有效的表达式将无法编译,因为 println 不接受多个参数.此行为还有其他有效用例.

If the compiler did not do this, perfectly valid expressions like Console println (1,2) would fail to compile because println does not take multiple arguments. There are also other valid use cases for this behavior.

从编译器的角度考虑像 foo bar (1,2) 这样的表达式,记住 Scala 具有允许您删除 的特殊语法.以及方法调用的括号.这可以是对带有参数 12 的双参数 bar 方法的调用,也可以是对单参数的调用bar 方法,带有单个元组值参数.解析器对 bar 方法一无所知,因此它只是将其解析为两个参数的方法调用.

Consider an expression like foo bar (1,2) from the compiler's point of view, keeping in mind that Scala has special syntax that allows you to drop the . and the parens on method calls. This could be a call to a two-argument bar method with arguments 1 and 2, or it could be a call to a one-argument bar method with a single tuple-valued argument. The parser doesn't know anything about the bar method, so it just parses as a two-argument method call.

在类型检查阶段,假设编译器确定 foo 没有双参数 bar 方法,但它确实有一个单参数 bar 方法,其签名与元组解释兼容.由于没有其他有效的解释,它假定这就是您的意思并将两个参数转换为一个元组.请注意,如果有两个参数的 bar 方法,即使是与实际参数不兼容的方法,类型器 也不会执行自动元组转换.

During the type checking phase, suppose the compiler determines that foo has no two-argument bar method but that it does have a one-argument bar method whose signature is compatible with the tuple interpretation. Since there is no other valid interpretation, it assumes that this is what you meant and transforms the two arguments into a tuple. Note that if there is a two-argument bar method, even one that is incompatible with the actual arguments, the typer will not perform the auto-tupling transformation.

这篇关于为什么以及如何在调用单参数函数时特别处理元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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