[swift-evolution] [Proposal]: Escaping another (unused) scope pyramide with 'guard try catch'

Adrian Zubarev adrian.zubarev at devandartist.com
Sat Feb 6 01:28:34 CST 2016


It is possible in a lighter project, but its no fun to build an ErrorType for this: http://www.opensource.apple.com/source/xnu/xnu-1456.1.26/bsd/sys/errno.h

Here is a snippet from my current project I’m working on:

public enum TCPBug: ErrorType {
		
	case NetworkError(code: Int32, message: String)
	case FileError(code: Int32, message: String)
		
	@noreturn private static func raise(type: TCPBugType) throws {
			
		let errorCode = errno
		let errorMessage = String.fromCString(strerror(errorCode))!
			
		switch type {
				
		case .Network:
			throw TCPBug.NetworkError(code: errorCode, message: errorMessage)
		case .File:
			throw TCPBug.FileError(code: errorCode, message: errorMessage)
		}
	}
}

As you might guess from errno.h there are a lot a different errors that could be thrown. And as I described in my previous post I could handle errors where they occur, so there won’t be any new pyramid. 

Anyways, from my perspective Swift does need a single 'do try catch‘ mechanism which escapes the 'do { }‘ scope. The only one problem we found is that declaring a new constant/variable inside a single ‘do try catch’ statement can not fall through from the catch body (like the 'guard' mechanism).

Best regards, 
Adrian Zubarev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160206/6fe888ce/attachment.html>


More information about the swift-evolution mailing list