Swift: Protocol requirement for nullary init/func cannot be met with default arguments

Originator:rix.rob
Number:rdar://18547983 Date Originated:04-Oct-2014 03:56 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode-6.1 (6A1042b)
Classification:Other Bug Reproducible:Always
 
Summary:
ExtensibleCollectionType requires init(). I want my default initializer to optionally take a collection to initialize with. I can’t do this via default arguments; I have to provide two initializers instead.

The same applies to function requirements.

This is surprising and necessitates boilerplate.


Steps to Reproduce:
1. Try to compile this code:

protocol P {
	init()

	func f()
}

struct S1 : P {
	init() {}
	func f() {}
}

struct S2 : P {
	init(_ x: Int = 0) {}
	func f(_ x: Int = 0) {}
}



Expected Results:
“Build succeeded”


Actual Results:
./Protocol-default-args.swift:14:1: error: type 'S2' does not conform to protocol 'P'
struct S2 : P {
^
./Protocol-default-args.swift:4:2: note: protocol requires initializer 'init()' with type '()'
        init()
        ^
./Protocol-default-args.swift:15:2: note: candidate has non-matching type '(Int)'
        init(_ x: Int = 0) {}
        ^
./Protocol-default-args.swift:6:7: note: protocol requires function 'f()' with type '() -> ()'
        func f()
             ^
./Protocol-default-args.swift:16:7: note: candidate has non-matching type '(Int) -> ()'
        func f(_ x: Int = 0) {}
             ^


Regression:
N/A


Notes:
N/A

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!