[swift-evolution] SE-0080: Failable Numeric Conversion Initializers

William Shipley wjs at mac.com
Thu May 5 02:42:35 CDT 2016


My quibble with this proposal (which should go through in one form or another) is that the initializers don’t throw.

I know that this was discussed before, but was this before "try?” was introduced to Swift? Because to me this seems cleaner (with init(exact value: Float) throws)

    let x = try? Int(exact: someFloat)

Than this (with init?(exact value: Float))

    let x = Int(exact: someFloat)


Let me be clear I’m not saying no initializers should ever be optional, just that with numeric conversions, it seems like the common case is that we got some bad data, and I like the “try?” syntax for noting, “Hey, this would mean something bad has happened.”

I also like ‘throws' because it could return something sensible so we could debug the bad input, whereas just returning ‘nil’ doesn’t really tell me why the conversion didn’t occur. In the example given (parsing through a json file) I’d much rather be able to tell the user “Warning, your file is corrupt: 4372498293429387283497824 is too big for your current net worth!” than “ Warning, your file is corrupt: 4372498293429387283497824 is somehow not good enough but I can’t really tell you why.”

-W
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160505/105a860c/attachment.html>


More information about the swift-evolution mailing list