scala 将多个函数调用参数合并为一个元组——这可以禁用吗? [英] scala coalesces multiple function call parameters into a Tuple -- can this be disabled?

查看:31
本文介绍了scala 将多个函数调用参数合并为一个元组——这可以禁用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这在我的项目中违反了类型安全,因此我正在寻找一种方法来禁用它.似乎如果一个函数采用 AnyRef(或 java.lang.Object),您可以使用任何参数组合调用该函数,Scala 会将参数合并为一个 Tuple 对象并调用该函数.

This is a troublesome violation of type safety in my project, so I'm looking for a way to disable it. It seems that if a function takes an AnyRef (or a java.lang.Object), you can call the function with any combination of parameters, and Scala will coalesce the parameters into a Tuple object and invoke the function.

在我的情况下,该函数不需要元组,并且在运行时失败.我希望这种情况会在编译时被发现.

In my case the function isn't expecting a Tuple, and fails at runtime. I would expect this situation to be caught at compile time.

object WhyTuple {
 def main(args: Array[String]): Unit = {
  fooIt("foo", "bar")
 }
 def fooIt(o: AnyRef) {
  println(o.toString)
 }
}

输出:

(foo,bar)

推荐答案

这里根本没有隐式或 Predef —— 只是很好的老式编译器魔法.您可以在 类型检查器.我现在无法在规范中找到它.

No implicits or Predef at play here at all -- just good old fashioned compiler magic. You can find it in the type checker. I can't locate it in the spec right now.

如果你有足够的动力,你可以在编译器中添加一个 -X 选项来防止这种情况发生.

If you're motivated enough, you could add a -X option to the compiler prevent this.

或者,您可以避免编写接受 TupleN 超类型的 arity-1 方法.

Alternatively, you could avoid writing arity-1 methods that accept a supertype of TupleN.

这篇关于scala 将多个函数调用参数合并为一个元组——这可以禁用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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