Swift 2.0b4: Allow assignment of nil to self in failable initializers to indicate failure

Originator:rix.rob
Number:rdar://22108390 Date Originated:02-Aug-2015 04:19 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode-beta (7A165t)
Classification:Enhancement Reproducible:Always
 
Summary:
In rdar://problem/22108354 I wrote a (pseudoSwift) example of rewriting a static function into a failable initializer:

init?(_ weave: (A -> Location?) -> A -> Location?, _ up: A -> Location?, _ a: A) {
	func into(t1: A) -> Location? {
		return Location(it: t1, down: weave(into), up: up, left: const(nil), right: const(nil))
	}
	self = into(a)
}

Unfortunately this does not compile, because the assignment of an Optional type to self is illegal. Instead, one has to write:

init?(_ weave: (A -> Location?) -> A -> Location?, _ up: A -> Location?, _ a: A) {
	func into(t1: A) -> Location? {
		return Location(it: t1, down: weave(into), up: up, left: const(nil), right: const(nil))
	}
	if let location = into(a) {
		self = location
	} else {
		return nil
	}
	// note: one can use an equivalent guard clause:
	// guard let location = into(a) else { return nil }
	// self = location
}

While this has the virtue of actually compiling today, it’s a lot of extra syntax to do essentially the same thing: return `nil` if the assigned value was `nil`, or else assign the (non-`nil`) value.


Steps to Reproduce:
N/A

Expected Results:
N/A

Actual Results:
N/A

Regression:
N/A

Notes:
This would presumably only apply to value types since if I recall correctly assignment to self is only legal in value type initializers.

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!