[swift-evolution] Properties on Default Protocol Implementations

Wallacy wallacyf at gmail.com
Sun Jan 10 20:43:54 CST 2016


Thinking about some problems presented here this mailing list. I believe
that by following the same concepts behind the default protocol
implementations, allowing the same mechanism to provide default properties
can be a remarkable gain for language.


It has been proposed here also on this list, a need to produce abstract
classes, one of the reasons that need, is because is not possible to
declare properties in the same way as we declare default implementation on
I also believe that this can help in the concept of multiple inheritance,
and serve as an aid to the default implementation on protocols, and
"complete" the Protocol-Oriented Programming concept.

For example:

protocol Named {
    var name: String { get }
protocol Aged {
    var age: Int { get }
struct Person: Named, Aged {
    var name: String
    var age: Int

extension Aged where Self: Named {
    func wishHappyBirthday() { // regular default implementation
        print("Happy birthday \(self.name) - you're \(self.age)!")
    var birthdayVideo: AVPlayerItem? // nil is a default value
func playBirthdayMediaFrom(person: Person){
    var avPlayer = AVPlayer(playerItem: person.birthdayVideo)
    // etc...

One of thousands of using this feature is to prevent us to create variables
that are not actually part of the data model we are shaping.

birthdayVideo in this case would be any variable that is not part of our
model, but need to be associated with the object (or structure) in some
context of our application. (And not be used anywhere else in the APP or
another API).

Other examples maybe a counter helper, weak reference to something, etc.
There is a infinite examples when we need to declare some variable just to
make the "api" happy like add a observer, holding some value, and use this
again to removeobserver in dealloc.

I believe that the same rules and the same mechanisms involving default
implementation functions, should govern this default property
implementation, and any discussion about it on the problems on protocols
rules should be made separate this thread.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160111/99a20737/attachment.html>

More information about the swift-evolution mailing list