从Scala项目中排除特定的隐式

问题描述 投票:7回答:2

如何防止在我的scala代码中使用特定的隐式?

例如,我最近是由Codec提供的默认https://github.com/scala/scala/blob/68bad81726d15d03a843dc476d52cbbaf52fb168/src/library/scala/io/Codec.scala#L76。有没有办法确保任何调用implicit codec: Codec的代码永远不会使用fallbackSystemCodec提供的代码?或者,是否可以阻止所有隐式编解码器?

这是使用scalafix应该可行的吗?

scala sbt implicit-conversion
2个回答
5
投票

Scalafix可以使用inspect implicit arguments SemanticTreeHere是一个定义自定义scalafix规则的示例解决方案。

特定

import scala.io.Codec

object Hello {
  def foo(implicit codec: Codec) = 3
  foo
}

我们可以定义自定义规则

class ExcludedImplicitsRule(config: ExcludedImplicitsRuleConfig)
    extends SemanticRule("ExcludedImplicitsRule") {

...

  override def fix(implicit doc: SemanticDocument): Patch = {
    doc.tree.collect {
      case term: Term if term.synthetic.isDefined => // TODO: Use ApplyTree(func, args)
        val struct = term.synthetic.structure
        val isImplicit = struct.contains("implicit")
        val excludedImplicit = config.blacklist.find(struct.contains)
        if (isImplicit && excludedImplicit.isDefined)
          Patch.lint(ExcludedImplicitsDiagnostic(term, excludedImplicit.getOrElse(config.blacklist.mkString(","))))
        else
          Patch.empty
    }.asPatch
  }

}

和相应的.scalafix.conf

rule = ExcludedImplicitsRule
ExcludedImplicitsRuleConfig.blacklist = [
  fallbackSystemCodec
]

应该启用sbt scalafix来提高诊断

[error] /Users/mario/IdeaProjects/scalafix-exclude-implicits/example-project/scalafix-exclude-implicits-example/src/main/scala/example/Hello.scala:7:3: error: [ExcludedImplicitsRule] Attempting to pass excluded implicit fallbackSystemCodec to foo'
[error]   foo
[error]   ^^^
[error] (Compile / scalafix) scalafix.sbt.ScalafixFailed: LinterError

注意println(term.synthetic.structure)的输出

Some(ApplyTree(
  OriginalTree(Term.Name("foo")),
  List(
    IdTree(SymbolInformation(scala/io/LowPriorityCodecImplicits#fallbackSystemCodec. => implicit lazy val method fallbackSystemCodec: Codec))
  )
))

显然,上述解决方案在搜索字符串时效率不高,但它应该给出一些指导。或许在ApplyTree(func, args)上匹配会更好。

scalafix-exclude-implicits-example演示了如何配置项目以使用ExcludedImplicitsRule


2
投票

您可以通过一起使用新类型来完成此操作;这样,没有人能够在您的依赖项中覆盖它。这基本上就是我发布给create an ambiguous low priority implicit的答案

但是,如果您无法更改类型,则可能不实用。

© www.soinside.com 2019 - 2024. All rights reserved.