scala - 为什么方法参数化而类型参数从不使用?

标签 scala scalatest

在 ScalaTest 中有一个 org.scalatest.selenium.WebBrowser trait 提供了这个方法:

def executeScript[T](script: String, args: AnyRef*)(implicit driver: WebDriver): AnyRef =
    driver match {
      case executor: JavascriptExecutor => executor.executeScript(script, args.toArray : _*)
      case _ => throw new UnsupportedOperationException("Web driver " + driver.getClass.getName + " does not support javascript execution.")
    }

我很好奇为什么在实现中没有使用类型参数T时,这个方法被参数化了。文档没有说明这方面的任何内容。

你能澄清一下吗?

最佳答案

根据 Semmle :

An unused type parameter will have no effect and always be inferred to Nothing.

它还提供了以下建议:

Unused type parameters have no effect on your program beyond confusing the reader and cluttering up the definition... Remove the type parameter if it is genuinely unused, or add a use if it is supposed to be used.

关于scala - 为什么方法参数化而类型参数从不使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49922896/

相关文章:

scala - 使用 FunSuite 测试 Spark 抛出 NullPointerException

scala - 将带有结构类型键的映射传递到 Spark UDF

scala - 如何创建 sbt 任务来生成代码,然后将这些生成的托管源包含在我的根项目中?

scala - 如何使用 sbt-scoverage 在 Akka-HTTP 服务中编写集成测试用例?

Scalatest "At least one"代替 Forall

testing - 你能在 scala 中测试嵌套函数吗?

scala - 如何匹配 ScalaTest 中的实例类型

scala - 如何在scala上使用表测试?

scala - 为什么Scala的indexOf(在List等中)返回Int而不是Option [Int]?

scala - Scala 中可变集合的迭代器?