问题描述
我查看了这个问题试图更好地理解scala中的 @tailrec
注释.
I looked at this question trying to better understand @tailrec
annotation in scala.
我不确定是注解是否还提示编译器进行了一些优化,还是仅在标记了非尾递归的方法时才用于警告?
What I'm not sure is whether the annotation also hints the compiler to do some optimizations or it's only used for warnings when you mark a method that is not a tail-recursion?
更具体地说-此注释是否可能以任何方式影响性能?例如,如果我不添加此批注,则编译器会将尾递归函数编译为非尾递归吗?
More specifically - is this annotation might affect performance in any way? For example, if I don't put this annotation, the compiler will compile a tail-recursive function as non-tail recursive?
推荐答案
根据scaladoc:
As per the scaladoc:
如果存在,则编译器将在方法中发出错误无法优化成循环.
If it is present, the compiler will issue an error if the method cannot be optimized into a loop.
这是一个验证,如果您认为实际上未编写可优化的函数,则触发错误.即使您不放它,编译器也会尽可能地优化代码.
This is a verification to trigger an error if you thought you wrote an optimizable function while you actually did not. Even if you don't put it, the compiler will optimize the code if it is possible.
这篇关于@tailrec是否会影响编译器优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!