为什么方法参数化而类型参数从不使用?
Why method is parameterized while type parameter is never used?
在 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.
在 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.