Can I simulate traits/mixins in Swift?

前端 未结 5 658
南旧
南旧 2021-02-01 16:45

Does Swift have a way of mixing in traits, a la Scala? The section of the Swift book on using extensions to add protocols to existing classes comes tantalizingly close. However,

5条回答
  •  暖寄归人
    2021-02-01 17:15

    As of Swift 2.0, yes!

    Providing Default Implementations

    You can use protocol extensions to provide a default implementation to any method or property requirement of that protocol. If a conforming type provides its own implementation of a required method or property, that implementation will be used instead of the one provided by the extension.

提交回复
热议问题