[swift-evolution] Type Inferencing For Error Handling (try catch blocks)

Manav Gabhawala manav1907 at gmail.com
Fri Dec 4 18:04:15 CST 2015


First of all, Swift is an absolute dream of a programming language and is really taking the future of programming in the right direction.

However, it seems backwards to me that everything in swift is type safe and complete (like enum cases) but error handling seems lacking on that end. It would be a huge feature if the compiler could infer the types of errors functions throw and make sure you catch all the different types of errors at the call site. The compiler could also chain the types through multiple throwing function callers. This feature would require no extra work on the programmers part because the compiler would infer the types of errors being thrown. For example,


func foo() throws
{
	...
	throw ErrorType1.BadRequest
	...
	throw ErrorType1.NoAuthentication
	...
	throw ErrorType2.NoResourceFound
}
func main()
{
	do 
	{
		foo()
	}
	catch (ErrorType1.BadRequest)
	{
			
	}
	catch (ErrorType2.NoResourceFound)
	{
	}
	// Compiler error because ErrorType1.NoAuthentication is not included.
}

Errors can also be chained together such that each function statically identifies the types thrown and if a call is made to a throwing function all the uncaught types are propagated out of the call stack. This would firstly be beneficial because we wouldn’t need the ridiculous empty catch { } block at the end that most people end up doing and secondly would ensure programmers are completely and always aware the possible errors that can be thrown by the function calls they make.

Regards,
Manav
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151204/1a38a3a4/attachment.html>


More information about the swift-evolution mailing list