Swift struct type recursive value

前端 未结 4 1802
春和景丽
春和景丽 2020-12-11 17:46

Structs can\'t have recursive value types in Swift. So the follow code can\'t compile in Swift

struct A {
    let child: A
}

A value type c

4条回答
  •  醉梦人生
    2020-12-11 18:12

    I think it's about the required space.

    Infinite space

    To create a value of this type

    struct A {
        let child: A
    }
    

    we need

    • the space for the current struct
    • the space for the child
    • the space for the child's child
    • ...

    So we need infinite space.

    Finite space

    On the other hand to create a value of this

    struct A {
        let children: [A]
    }
    

    we only need

    • the space for A
    • the space for an empty Array.

提交回复
热议问题