作者:横着走觉察生活_915 | 来源:互联网 | 2023-02-05 19:48
根据Scala语言规范的第6.19节,这个for循环:
for (e <-p) e'
被翻译成:
p <- e.withFilter{case p => true; case _ => false}.foreach{case p => e?}
那么,为什么这个小程序:
object ForAndPatterns extends App {
class A()
class B() extends A
val list: List[A] = List(new A(), new B(), new B())
for {b: B <- list}
println(b)
}
给出了这个编译错误:
Error:(7, 13) type mismatch;
found : proves.ForAndPatterns.B => Unit
required: proves.ForAndPatterns.A => ?
for {b: B <- list}
当这个表达式:
list.withFilter{case a: B => true; case _ => false}.foreach{case b => println(b)}
没有错误.
1> Alexey Roman..:
实际上,您从规范中获得的翻译
list.withFilter{case b: B => true; case _ => false}.foreach{case b: B => println(b)}
但它仍然编译和工作.似乎Scala正在失去case
并转化为
list.withFilter{case b: B => true; case _ => false}.foreach{b: B => println(b)}
会产生同样的错误.
事实证明这是一个已知的旧bug:https://github.com/scala/bug/issues/900.
解决方法提供:
object Typed { def unapply[A](a: A) = Some(a) }
for { Typed(b: B) <- list } println(b)