Conflicting definition of Swift struct and array

后端 未结 6 1020
孤街浪徒
孤街浪徒 2021-02-01 09:56

In Swift Programming Language, it says:

  1. “all of the basic types in Swift—integers, floating-point numbers, Booleans, strings, arrays and dictio

6条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-01 10:54

    Array are just value types. That is correct. And it is very likely to use COW(Copy-On-Write) to avoid high copying cost of brute-force value-type semantics implementation. So it will be copied only when the new instance is going to be mutated.

    There was some trouble with this semantics issue in beta period, but it was eliminated in later betas because having a grey-colored behaviour is insane, and now (Swift version 1.0) it is strictly value type semantics.

    Your point #3 is just an explanation about the optimisation --- which is an implementation details meaningful only for optimisations. Forget about implementation details when you thinking about semantics. Just use arrays and dictionaries just like any other value-type stuffs. And remember the implementation details only when you need to concern about the performance and optimisation. In most cases, just using is fine in both of semantics or performance perspectives.

    BTW, reference type means it has automatic/implicit identity that can be referenced.

提交回复
热议问题