Support for `defer if ` feature

Originator:pitiphong.ph
Number:rdar://21548720 Date Originated:26-Jun-2015 12:58 AM
Status:Open Resolved:
Product:Developer Tools Product Version:Swift 2.0
Classification:Enhancement Reproducible:Not Applicable
 
Summary:
Support for `defer if ` which will call defer block only if the condition are true which including throwing some error

I think there are some cases which this feature might works for example

func createGroupFromData(data: T) -> Handler {
	guard if let queue = dispatch_queue_create() else { throws error }
	defer if throws error {
		dispatch_queue_release(queue)
	}
	guard if let file = open(fd) else { throws error }
	defer if throws error {
		close(fd)
	}

	if let handler = data.handlerWithQueue(queue, file: file) {
		return handler
	} else {
		throws error // Call the previous 2 defer blocks only if this error is thrown
	}
}

This is an @schwa ’s idea and I think it would be very useful in some case.

Regression:

Notes:
Credit to @schwa

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!