英文:
In Scala 2, what are possible ways to write a shortcut of a partial function without triggering unchecked warning?
问题
这是一个关于之前问题的后续提问:
我正在尝试在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:
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 <: String
Match("abc": Serializable).apply(
{ case v: Unknown => 1 },
{ case v: Any => 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/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 <: String
Match("abc": Serializable).apply(
{ case v: Unknown @unchecked => 1 },
{ case v: Any => 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/63998833/scala-pattern-matching-on-a-path-dependent-type
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论