[swift-evolution] Making protocol conformance inheritance controllable

Michel Fortin michel.fortin at michelf.ca
Thu Dec 10 22:04:34 CST 2015


Le 10 déc. 2015 à 21:04, Joe Groff via swift-evolution <swift-evolution at swift.org> a écrit :

> And if a class hierarchy really wants to impose a conformance on all possible subclasses, as happens today, we could let you opt in to that:
> 
> class E: required Fungible {
>   // init() must be required of all subclasses
>   required init() { }
> 
>   // funged() must return a covariant object
>   class func funged() -> Self { return Self() }
> }

I think the concept makes sense. But you're changing the default behaviour here. Is this because you expect protocols should not be inherited most of the time? That most protocols in Cocoa shouldn't be inherited? That goes against "obvious way things should be".

Also, shouldn't this be allowed too?

	class F: C, required override Fungible {
		required init() { }
		class func funged() -> Self { return Self() }
	}

Personally, I'd tend to make 'required' the default mode, and use "static Fungible" for the odd one that does not apply to subtypes. And I'd get rid of override before the protocol name, just use "static Fungible" again in the subclass:

	class C: static Fungible {
		init() {}
		class func funged() -> C { return C() }
	}
	class D: C, static Fungible {
		init() {}
		override class func funged() -> D { return D() }
	}
	class E: Fungible {
		required init() {}
		class func funged() -> Self { return Self() }
	}
	class F: C, Fungible {
		required init() {}
		override class func funged() -> Self { return Self() }
	}

But in the end the syntax depends on what default behaviour is desired.

-- 
Michel Fortin
michel.fortin at michelf.ca
https://michelf.ca



More information about the swift-evolution mailing list