Scala:类型注释使尾递归检查失败 [英] Scala: type annotations make tail recursion check fail
问题描述
我在此模式匹配中添加类型注释只是为了我自己的理解.
I'm adding type annotations to this pattern match just for my own understanding.
@annotation.tailrec def run[A](io: IO[A]): A = {
io match {
case Return(a) => a
case Suspend(r) => r()
case FlatMap(x, f) => x match {
case Return(a) => run(f(a))
case Suspend(r) => run(f(r()))
case FlatMap(y, g) =>
run(y flatMap (a => g(a) flatMap f))
}
}
}
为什么这些类型注解会破坏尾递归检查?添加新的类型定义和类型注释后,我没有清楚地看到代价高昂的新递归.
Why do these type annotations break the tail recursion check? With the new type definitions and type annotations added, I don't clearly see a costly new recursion.
could not optimize @tailrec annotated method run: it contains a recursive call not in tail position
io match {
^
@annotation.tailrec def run[A](io: IO[A]): A = {
type rType = Unit => A
type fType = A => IO[A]
type gType = A => IO[A]
io match {
case Return(a: A) => a
case Suspend(r: rType) => r()
case FlatMap(x: IO[A], f: fType) => x match {
case Return(a: A) => run(f(a))
case Suspend(r: rType) => run(f(r()))
case FlatMap(y: IO[A], g: gType) =>
run(y flatMap (a => g(a) flatMap f))
}
}
}
匹配的案例类:
case class Return[A](a: A) extends IO[A]
case class Suspend[A](resume: () => A) extends IO[A]
case class FlatMap[A,B](sub: IO[A], k: A => IO[B]) extends IO[B]
只要省略类型注解,类型'a'就行
As long as the type annotations are omitted, type of 'a' in line
F.flatMap(r)((a: A) => run(f(a)))
必须是任何":
[error] found : A => F[A]
[error] required: Any => F[A]
[error] F.flatMap(r)((a: A) => run(f(a)))
编译:
F.flatMap(r)(a => run(f(a)))
<小时>
奖励问题.
Bonus question.
似乎不允许对 case 类中的函数进行模式匹配,如下所示:
It seems that pattern matching against a function inside a case class, like this, is not allowed:
io match {
...
case Suspend(r: Unit => A) => r()
/* or */
case Suspend(r: () => A) => r()
...
}
编译:
io match {
...
case Suspend(r: Function0[A]) => r()
...
}
这是为什么?
由于类型擦除,这些类型注释最终不会有太大用处.注释这些类型后,我可以期待看到这样的编译器警告:
These type annotations won't have much use in the end because of type erasure. After annotating these types, I can expect to see a compiler warning like this:
abstract type pattern ... is unchecked since it is eliminated by erasure
<小时>
此代码来自Scala 中的函数式编程"的第 13 章或包 fpinscala.iomonad.https://github.com/fpinscala/fpinscala
谢谢
推荐答案
奖励答案:关于类型擦除的问题比较多,看看如何解决 Scala 上的类型擦除问题?或者,为什么我不能得到我的集合的类型参数?你可以写一些像
Bonus answer: There are a lot of questions about type erasure, take a look at How do I get around type erasure on Scala? Or, why can't I get the type parameter of my collections? You can write something like
case FlatMap(y: IO[A], g: gType@unchecked) if g.isInstanceOf[gType] =>
这篇关于Scala:类型注释使尾递归检查失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!