简体   繁体   English

在Scala中将'_'(下划线)理解为类型变量

[英]Understanding of '_' (underscore) in Scala as a type variable

In Scala I can use underscore as a type variable in function, for instance. 例如,在Scala我可以使用下划线作为函数中的类型变量。

  def fun[_, _]() = {
    // ?
  }

  fun[Int, String]()

Is it so useful in real life programming? 在现实生活中编程有用吗? How could it be useful for me? 对我有什么用?

Is it so useful in real life programming? 在现实生活中编程有用吗?

No, it isn't. 不,不是。

How could it be useful for me? 对我有什么用?

To confuse people. 使人迷惑。 You could theoretically use it to mark an unused type parameter when eg overriding a method, but I don't think I've ever seen this. 从理论上讲,您可以在覆盖方法时使用它来标记未使用的类型参数,但是我认为我从未见过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM