In Scala 2, what are possible ways to write a shortcut of a partial function without triggering unchecked warning?

huangapple go评论58阅读模式
英文:

In Scala 2, what are possible ways to write a shortcut of a partial function without triggering unchecked warning?

问题

这是一个关于之前问题的后续提问:

https://stackoverflow.com/questions/75440665/in-scala-3-why-runtime-pattern-matching-cant-work-reliably-on-duck-type-using

我正在尝试在Scala中创建一个更能够抵御类型擦除的模式匹配实现。它仍然应该仅依赖于JVM上类的反射,但在运行时进行健全性检查或类强制转换错误的情况下可以平稳降级到替代方案。

我的实现的签名如下:

@unchecked
case class Match[T](self: T) {

  def apply[R](
      @unchecked cases: PartialFunction[T, R]*
  ): R = ???
}

在测试涉及类型擦除的情况时:

trait H {

  type Unknown <: String

  Match("abc": Serializable).apply(
    { case v: Unknown => 1 },
    { case v: Any => 2 }
  )
}

我仍然收到警告消息:

[Warn] /xxx.scala:20:19: abstract type pattern Has.this.Unknown is unchecked since it is eliminated by erasure
[Warn] /xxx.scala:20:16: The outer reference in this type test cannot be checked at run time.

看起来@unchecked注解对于抑制编译器警告没有效果。这实际上排除了在我的定义中使用偏函数的可能性。

如何可以抑制这个警告?

英文:

This is a follow-up question of:

https://stackoverflow.com/questions/75440665/in-scala-3-why-runtime-pattern-matching-cant-work-reliably-on-duck-type-using

I'm trying to create a pattern matching implementation in Scala that is more resilient to type erasure. It should still rely on only JVM reflection on class but can degrade smoothly to alternatives in case of a runtime sanity check or class cast error.

The signature of my implementation looks like this:

@unchecked
case class Match[T](self: T) {

  def apply[R](
      @unchecked cases: PartialFunction[T, R]*
  ): R = ???
}

When testing it on a case that involves type erasure:

    trait H {

      type Unknown &lt;: String

      Match(&quot;abc&quot;: Serializable).apply(
        { case v: Unknown =&gt; 1 },
        { case v: Any =&gt; 2 }
      )
    }

I still got the warnings:

[Warn] /xxx.scala:20:19: abstract type pattern Has.this.Unknown is unchecked since it is eliminated by erasure
[Warn] /xxx.scala:20:16: The outer reference in this type test cannot be checked at run time.

It appears that @unchecked annotation has no effect on suppressing compiler message. This effectively rules out possibility to use partial function in my definition.

How can this warning be suppressed?

答案1

得分: 2

不好意思,你的要求是只返回翻译好的部分,不包括代码部分。以下是翻译好的内容:

"Well, you just put @unchecked in wrong places. It's for annotating types (making a type AnnotatedType internally). Try to annotate the type in type pattern. Usage example is in the scaladoc of @unchecked and in Scala spec."

"Scaladoc: https://www.scala-lang.org/api/2.13.10/scala/unchecked.html"

"Scala spec: https://www.scala-lang.org/files/archive/spec/2.13/11-annotations.html#scala-compiler-annotations"

https://stackoverflow.com/questions/54785723/what-is-correct-syntax-for-unchecked-in-a-fold-operation

https://stackoverflow.com/questions/51144912/scala-recursively-pattern-match-a-heterogeneous-list-obtaining-the-correct-typ

https://stackoverflow.com/questions/63998833/scala-pattern-matching-on-a-path-dependent-type

英文:

Well, you just put @unchecked in wrong places. It's for annotating types (making a type AnnotatedType internally). Try to annotate the type in type pattern. Usage example is in the scaladoc of @unchecked and in Scala spec.

case class Match[T](self: T) {
  def apply[R](
                cases: PartialFunction[T, R]*
              ): R = ???
}

trait H {
  type Unknown &lt;: String

  Match(&quot;abc&quot;: Serializable).apply(
    { case v: Unknown @unchecked =&gt; 1 },
    { case v: Any =&gt; 2 }
  )
}

Scaladoc: https://www.scala-lang.org/api/2.13.10/scala/unchecked.html

Scala spec: https://www.scala-lang.org/files/archive/spec/2.13/11-annotations.html#scala-compiler-annotations

https://stackoverflow.com/questions/54785723/what-is-correct-syntax-for-unchecked-in-a-fold-operation

https://stackoverflow.com/questions/51144912/scala-recursively-pattern-match-a-heterogeneous-list-obtaining-the-correct-typ

https://stackoverflow.com/questions/63998833/scala-pattern-matching-on-a-path-dependent-type

huangapple
  • 本文由 发表于 2023年2月14日 06:38:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441852.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定