[swift-evolution] Remove Failable Initializers

James Campbell james at supmenow.com
Thu Mar 3 02:19:28 CST 2016

Just to iterate returning an optional as an error I still think is fine for normal functions.
But for the reasons I outlined above I think it's problematic for inits.
Of course two simpler ways towards solving it would to have a guide to explain best practice of when to use fallible init vs a throw.
Also all falliable inits perhaps should have ? In their name (and maybe functions who return one too) Or at least be allowed to give the function a name with ? In it.
In Ruby ? And ! Were nice explicit ways of saying this returns a Bool or this does something dangerous. Having this in swift or something like it would be a great way of making optional functions easier but I'm not sure how desirable this is.
Hence my original proposal is just limited to inits where unexpected errors are far more likely to occur than a function.
Sent from Supmenow.com

On Thu, Mar 3, 2016 at 12:07 AM -0800, "Patrick Pijnappel via swift-evolution" <swift-evolution at swift.org> wrote:

The error handdling model as described in https://github.com/apple/swift/blob/master/docs/ErrorHandlingRationale.rst#kinds-of-error is very sensible and consistently applied throughout the language.
Returning an optional is a simplified form of returning an error when there is only one straightforward way of failing. This is a very common case and is simpler than using an error, for which you need to: 1) call w/ try? 2) add 'throws' to the function 3) return some kind of error instance of some type
On Thursday, 3 March 2016, David Hart via swift-evolution <swift-evolution at swift.org> wrote:
The reasoning for me is that they help describe the difference between a function that can return a nil value as part of it's normal operation and a function that can encounter an unexpected error.
On 03 Mar 2016, at 00:44, Ross O'Brien via swift-evolution <swift-evolution at swift.org> wrote:

At the risk of appearing glib or naive - which isn't my intention, I'd like to know the answer - is there not a similar argument to be made for any function which returns an optional instead of throwing a more descriptive error? Asking an array for its first element returns an optional because of the possibility it might have no elements in it; should this throw an error instead of being 'failable'?
On Wed, Mar 2, 2016 at 11:35 PM, Haravikk via swift-evolution <swift-evolution at swift.org> wrote:

On 2 Mar 2016, at 23:07, Chris Lattner via swift-evolution <swift-evolution at swift.org> wrote:
On Mar 2, 2016, at 1:11 PM, James Campbell via swift-evolution <swift-evolution at swift.org> wrote:

Given that we now have error handling and availability checks does it make sense to have Failable Initializers which date back to Swift 1.1?

Our error handling schema (https://github.com/apple/swift/blob/master/docs/ErrorHandlingRationale.rst#kinds-of-error) defines how error conditions are handled, and one important class of them (e.g. the "string to int" case) is best modeled as returning an optional.  This works really well in practice for functions/methods in general. 
Could you give an example of why failable is the better fit here? To me the following two statements are identical:
	let a = FailableType()	let b = try? ThrowableType()
Except that in the latter case the try? is more explicit about what is happening (and that it can fail), and I have the option of catching the error to find out more about what went wrong. With some optimisation it should be possible for try? to be just as efficient as a failable initialiser I think.
That said, the failable initialiser could have the same explicit call syntax if it required a trailing question-mark, e.g:
	let a = FailableType()?
As currently the only indicator is on the initialiser declaration itself. Still, when it comes to debugging I’ve found it very useful to force myself to use error handling instead, as it means I have to give reasons for why something failed, which can make it easier to track issues when they do arise.

swift-evolution mailing list

swift-evolution at swift.org


swift-evolution mailing list
swift-evolution at swift.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160303/7449a078/attachment.html>

More information about the swift-evolution mailing list