[swift-evolution] throws!

Jonathan Hull jhull at gbis.com
Mon Dec 7 03:29:19 CST 2015


For context, I just read this thread (that happened before I joined) which discusses the idea of having a variant of arithmetic which can throw on overflow:
https://lists.swift.org/pipermail/swift-evolution/2015-December/000292.html


I wonder if having a variant of ‘throws’ might allow this...  ‘throws!’ would act exactly like throws, but it could be called without ‘try’ (resulting in a crash  if it tries to throw).  Basically, any statement calling a function/operation with throws! would have the equivalent of an implicit ‘try!' unless there is an actual ‘try’.

It would allow the following (assuming + is marked ‘throws!’):

	let x = 2 + 3  // No crash, No exception

	let y = try Int.max + 1 // Exception, but no crash

	let z = Int.max + 1 // Crash!

	let w = try? Int.max + 1 // nil

Similarly, the thread mentions the desire for a throwing forced unwrap operator, and I think this allows that as well. Assuming the force unwrap operator is marked 'throws!’:

	let x:Int? = nil
	
	let y = try x! // Exception, but no crash
	
	let z = x! // Crash!


Anyway, this is all of the top of my head, so I am sure there is some issue I am missing.  I am not entirely sure that this would allow more good than evil overall, but I do like that the ! in ‘throws!’ signifies danger.  In the cases above, it is being used to take something which already crashes now, and allow the programmer to catch an exception instead of that crash if they think to look for it.

Thanks,
Jon


More information about the swift-evolution mailing list