[swift-evolution] two protocols with the same method name

Thorsten Seitz tseitz42 at icloud.com
Sun Jan 10 12:01:23 CST 2016


In Eiffel you would have to rename at least one of the conflicting members in the implementing class to distinguish the cases there.
When called through one of the protocols the correct renamed member would be used, i.e. (translated to pseudo-Swift):

protocol Marriageable {
	var ring: String? { get set }	// File name of image of this person's wedding ring.
}

protocol CallReceivable {
	var ring: String? { get set } 	// File name of ringtone to be used for this person.
}

// In this example I choose to rename both
struct Person: Marriageable rename ring to ringImage, CallReceivable rename ring to ringTone {
	var ringImage: String?
	var ringTone: String?
}

let friend: Person = ...
let m: Marriageable = friend
let callee: CallReceivable = friend

friend.ring 		// type error (the Person type only has vars ringImage and ringTone)
friend.ringImage  	// file name of image of wedding ring
friend.ringTone 	// file name of ring tone
m.ring 			// result of friend.ringImage because of rename
callee.ring 		// result of friend.ringTone because of rename

This always does the right thing and solves the problem nicely.

-Thorsten


> Am 09.01.2016 um 04:09 schrieb Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org>:
> 
>> I don’t really get what you are driving at.
> 
> The point is that, although `A` and `B` both require properties with the same name, they expect different semantics from that property. Let's maybe give these more concrete names so you can understand the idea:
> 
> 	protocol Marriageable {
> 		var ring: String? { get set }	// File name of image of this person's wedding ring.
> 	}
> 	protocol CallReceivable {
> 		var ring: String? { get set } 	// File name of ringtone to be used for this person.
> 	}
> 
> 	struct Person: Marriageable, CallReceivable {
> 		var ring: String?
> 	}
> 
> Of course a person is marriageable and can this have "a ring", and of course you can also receive a call from them and they can thus have "a ring". But in reality, the "ring" that satisfies one of these things will not work for the other. If your best friend gets married and you add an image of the ring, then the next time your friend calls you, the phone ringing screen will try to play a JPEG as an MP3.
> 
> The "ring" example is, of course, slightly contrived, but I'm sure you can imagine a similar problem with real names, where you end up using the same term for two different and incompatible things.
> 
> What the OP is basically asking is, when Swift sees the same type conforming to Marriageable and CallReceivable, should it optimistically assume that the `ring` properties they both require are compatible and allow the code to pass through without comment? Or should it pessimistically assume that the `ring` properties are incompatible and emit a warning or error about them?
> 
> -- 
> Brent Royal-Gordon
> Architechies
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160110/623370a7/attachment.html>


More information about the swift-evolution mailing list