gpt4 book ai didi

scala - scala 对推断类型的 "acceptable complexity"有什么限制?

转载 作者:行者123 更新时间:2023-12-01 06:25:22 27 4
gpt4 key购买 nike

根据Scala Language Spec :

... local type inference is permitted to limit the complexity of inferred bounds [of type parameters]. Minimality and maximality of types have to be understood relative to the set of types of acceptable complexity.



在实践中有何限制?

此外,适用于推断表达式类型的限制是否与适用于参数类型边界的限制不同,这些限制是什么?

最佳答案

在推断类型时,编译器通常需要计算类型列表的最小上限 (LUB)。例如if (cond) e1 else e1的类型是 e1 类型的 LUB和 e1 .

这些类型可能会变得非常大,例如在 REPL 中试试这个:

:type Map(1 -> (1 to 10), 2 -> (1 to 10).toList)
scala.collection.immutable.Map[Int,scala.collection.immutable.Seq[Int] with scala.collection.AbstractSeq[Int] with Serializable{def reverse: scala.collection.immutable.Seq[Int] with scala.collection.AbstractSeq[Int]{def reverse: scala.collection.immutable.Seq[Int] with scala.collection.AbstractSeq[Int]; def dropRight(n: Int): scala.collection.immutable.Seq[Int] with scala.collection.AbstractSeq[Int]; def takeRight(n: Int): scala.collection.immutable.Seq[Int] with scala.collection.AbstractSeq[Int]; def drop(n: Int): scala.collection.immutable.Seq[Int] with scala.collection.AbstractSeq[Int]; def take(n: Int): scala.collection.immutable.Seq[Int] with scala.collection.AbstractSeq[Int]}; def dropRight(n: Int): scala.collection.immutable.Seq[Int] with scala.collection.AbstractSeq[Int]{def reverse: scala.collection.immutable.Seq[Int] with scala.collection.AbstractSeq[Int]; def dropRight(n: Int): scala.collection.immutable.Seq[Int]...

commit引入了一些健全性检查来限制此类推断类型的深度。

最近有一些工作插入到编译过程中,以检测需要很长时间计算的推断类型,并建议显式类型注释可能谨慎的地方。

关于scala - scala 对推断类型的 "acceptable complexity"有什么限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11425350/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com