Swift struct type recursion

前端 未结 2 603
陌清茗
陌清茗 2021-02-07 11:24

Why can\'t structs have recursive value types in Swift? Is this a temporary limit of the language or is it as intended?

I feel that the ability to declare a binary tree

2条回答
  •  别跟我提以往
    2021-02-07 11:45

    Enums in Swift support recursive types using the indirect keyword so you can do something like:

    indirect enum Tree {
    
        case Node(left: Tree?, right: Tree?, element: T)
    
    }
    

    Check out this great blog post A persistent tree using indirect enums in Swift

提交回复
热议问题