[英]Excluding type evidence parameters from analysis in Scala when using -Ywarn-unused
Compiling a program that contains a type evidence parameter in Scala (such as T <:< U
) can cause a warning when -Ywarn-unused
is passed to the compiler. 当将
-Ywarn-unused
传递给编译器时,在Scala中编译包含类型证据参数的程序(例如T <:< U
)会引起警告。 Especially in the case when the type evidence parameter is used to verify a constraint encoded using phantom types, this warning is likely to occur. 特别是在类型证据参数用于验证使用幻像类型编码的约束的情况下,很可能会发生此警告。
As an example, compiling the file here: https://github.com/hseeberger/demo-phantom-types/blob/master/src/main/scala/de/heikoseeberger/demophantomtypes/Hacker.scala returns the following: 例如,在此处编译文件: https : //github.com/hseeberger/demo-phantom-types/blob/master/src/main/scala/de/heikoseeberger/demophantomtypes/Hacker.scala返回以下内容:
# scalac -Ywarn-unused Hacker.scala Hacker.scala:42: warning: parameter value ev in method hackOn is never used def hackOn(implicit ev: IsCaffeinated[S]): Hacker[State.Decaffeinated] = { ^ Hacker.scala:47: warning: parameter value ev in method drinkCoffee is never used def drinkCoffee(implicit ev: IsDecaffeinated[S]): Hacker[State.Caffeinated] = { ^ two warnings found
It's clear to me that the parameter ev
is not actually necessary at runtime, but the parameter is useful at compile time. 对我来说很明显,参数
ev
在运行时实际上不是必需的,但在编译时很有用。 Is there any way to instruct the compiler to ignore this case, while still raising the warning for unused function parameters in other contexts? 有什么方法可以指示编译器忽略这种情况,同时在其他情况下仍对未使用的函数参数提出警告?
For example, I think instructing the compiler to ignore implicit parameters of class <:<
or =:=
would solve this issue, but I'm not sure how that could be accomplished. 例如,我认为指示编译器忽略类
<:<
或=:=
隐式参数将解决此问题,但是我不确定如何实现。
I often find myself adding this because of either -Ywarn-unused
or -Ywarn-value-discard
: 我经常因为
-Ywarn-unused
或-Ywarn-value-discard
添加自己:
package myproject
package object syntax {
implicit class IdOps[A](a: A) {
def unused: Unit = ()
}
}
Lets you do ev.unused
in the code to explicitly "specify" that the value is not going to be used or is only there for side effects. 使您可以在代码中使用
ev.unused
来显式“指定”该值将不被使用或仅用于副作用。 You're not using class field in the definition, but that's okay for -Ywarn-unused
. 您没有在定义中使用类字段,但是
-Ywarn-unused
。
Your other option is to use silencer plugin to suppress warnings for these few methods. 您的另一个选择是使用消音器插件来抑制这几种方法的警告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.