Swift struct type recursive value

前端 未结 4 1812
春和景丽
春和景丽 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:16

    Since child[] can be empty there is no reason why this shouldn't work. This compiles just fine:

    struct Foo {
      let child: [Foo]
    }
    
    let foo = Foo(child: [Foo(child:[Foo(child:[]), Foo(child:[])])])
    

    Although I don't see any practical use for it – I wonder if there is, though.

提交回复
热议问题