Swift struct type recursion

前端 未结 2 606
陌清茗
陌清茗 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:46

    The answer is in your question: structs are value types. If you include a substruct B into a struct A, it means, that one object of type A will have a size sizeof(all_other_fields_of_A) + sizeof(B). So, a value type can not be recursive: it would have infinite size.

提交回复
热议问题