gpt4 book ai didi

swift - Swift 中递归枚举的奇怪行为(Beta 7)

转载 作者:搜寻专家 更新时间:2023-10-31 08:12:26 25 4
gpt4 key购买 nike

enum Tree{
case Leaf(String)
case Node(Tree)
} //compiler not happy!!

enum Tree{
case Leaf(String)
case Node([Tree])
} //compiler is happy in (arguably) a more complex recursive scenario?

Swift 编译器如何适用于第二种(更复杂的)场景而不是第一种?

最佳答案

值得注意的是 Swift 2 beta 2 和进一步具有用于递归枚举的 indirect 关键字 - 这意味着

enum Tree<T> {
case Leaf(T)
indirect case Node(Tree)
}

是有效的语言结构,不会破坏 Swift 2 中的模式匹配。

决定的 TL;DR:“[…] 我们认为正确的解决方案是简单地不支持通过枚举的一般的、不明显的递归,并要求程序员用间接的方式显式调解。”


关于swift - Swift 中递归枚举的奇怪行为(Beta 7),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25653541/

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