[swift-evolution] Properties on Default Protocol Implementations

Brent Royal-Gordon brent at architechies.com
Tue Jan 12 01:11:29 CST 2016


> An automatic approach brings hesitation to me, because I don’t want it to be the difference between:
> 
> protocol P {
>  var x : Int { get }
> }
> 
> and:
> 
> protocol P {
>  var x : Int 
> }
> 
> Did I want a PwS there, or did I just forget {get/set}?

If we're going to start adding concrete things to protocols—not just stored properties, but (drawing on other proposals) perhaps also default implementations, final members, factory initializers, etc.—perhaps we should start marking the things that *aren't* concrete with `required`.

Currently:

	public protocol Strideable : Comparable, _Strideable {
	    associatedtype Stride : SignedNumberType
	    @warn_unused_result
	    public func distanceTo(other: Self) -> Self.Stride
	    @warn_unused_result
	    public func advancedBy(n: Self.Stride) -> Self
	}
	
	extension Strideable {
	    @warn_unused_result
	    public func stride(to end: Self, by stride: Self.Stride) -> StrideTo<Self> {
	        /* implementation here */
	    }
	}
	
	extension Strideable {
	    @warn_unused_result
	    public func stride(through end: Self, by stride: Self.Stride) -> StrideThrough<Self> {
	        /* implementation here */
	    }
	}

Future:

	public protocol Strideable : Comparable, _Strideable {
	    associatedtype Stride : SignedNumberType
	    @warn_unused_result
	    required public func distanceTo(other: Self) -> Self.Stride
	    @warn_unused_result
	    required public func advancedBy(n: Self.Stride) -> Self
	    
	    @warn_unused_result
	    final public func stride(to end: Self, by stride: Self.Stride) -> StrideTo<Self> {
	        /* implementation here */
	    }
            
	    @warn_unused_result
	    final public func stride(through end: Self, by stride: Self.Stride) -> StrideThrough<Self> {
	        /* implementation here */
	    }
	}

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list