有些常见类型不会触发这个警告,没有成员的类型(“标记接口”)也一样。
比如 Comparable 会触发警告。如果根本没比较任何东西,可能是约束已经没用了,也可能是忘记比较了。
scala> def inspect[A: Comparable] = "57".toInt
1 warning found
-- [E198] Unused Symbol Warning: -----------------------------------------------
1 |def inspect[A: Comparable] = "57".toInt
| ^
| unused implicit parameter
def inspect[A](using evidence$1: Comparable[A]): Int
我觉得得在展开后的形式里明确标为未使用。
scala> def inspect[A](item: A)(using @unused witness: Comparable[A]) = item.toString
def inspect[A](item: A)(using witness: Comparable[A]): String
这比 @nowarn 稍微清楚一点,不过后者能用于上下文界限的写法。
(我猜按名称检查是在可以选择下面选项之前的办法
-Wunused:explicits,implicits,synthetics
其实 Scala 3 目前还没有 synthetics,只有 Scala 2 有。
-Wunused:synthetics Warn if a synthetic implicit parameter (context bound) is unused.
-Wunused:nowarn Warn if a @nowarn annotation does not suppress any warnings.
-Wunused:params Enable -Wunused:explicits,implicits,synthetics.
它是在疫情初期加进去的,大概是为了打发时间吧。)
从英语翻译·查看原文Certain familiar types are exempt from this warning, as are memberless types (“marker interfaces”).
Comparable, for instance, triggers it. If nothing gets compared, perhaps the constraint has become unnecessary, or perhaps you neglected to make a comparison.
scala> def inspect[A: Comparable] = "57".toInt
1 warning found
-- [E198] Unused Symbol Warning: -----------------------------------------------
1 |def inspect[A: Comparable] = "57".toInt
| ^
| unused implicit parameter
def inspect[A](using evidence$1: Comparable[A]): Int
I believe you have to mark it unused in this expanded version:
scala> def inspect[A](item: A)(using @unused witness: Comparable[A]) = item.toString
def inspect[A](item: A)(using witness: Comparable[A]): String
That's somewhat cleaner than @nowarn, although the latter accommodates context-bound notation.
(I suspect name-based checks date to before selecting
-Wunused:explicits,implicits,synthetics
In fact, synthetics isn't available in Scala 3 yet, only Scala 2:
-Wunused:synthetics Warn if a synthetic implicit parameter (context bound) is unused.
-Wunused:nowarn Warn if a @nowarn annotation does not suppress any warnings.
-Wunused:params Enable -Wunused:explicits,implicits,synthetics.
That arrived near the pandemic's beginning, presumably as a way of filling the hours.)