[swift-evolution] Remove Failable Initializers
Chris Lattner
clattner at apple.com
Thu Mar 3 15:01:34 CST 2016
On Mar 3, 2016, at 1:09 AM, James Campbell via swift-evolution <swift-evolution at swift.org> wrote:
>
> True but in my example above for parsing a JSON into a model I can do this:
>
> class Model {
>
> let property: String
> let property1: String
> let property2: String
> let property3: String
>
> init?(json: [String: AnyObject]) {
>
> guard property = json["property"] as? String else {
> return nil
> }
>
> guard property = json["property1"] as? String else {
> return nil
> }
>
> guard property = json["property2"] as? String else {
> return nil
> }
>
> guard property = json["property3"] as? String else {
> return nil
> }
Just FYI, but you can of course write this as:
guard let property = json["property"] as? String,
let property2 = json["property1"] as? String,
let property3 = json["property2"] as? String,
let property4 = json["property3"] as? String else {
return nil
}
-Chris
>
> }
>
> }
>
> There is a lot of boilerplate for very little information and I'm worried by letting people initialize without returning an error we will get complex objects like this being constructed and failing for no reason.
>
> Compare this to error handling:
>
> class Model {
>
> let property: String
> let property1: String
> let property2: String
> let property3: String
>
> init?(json: [String: AnyObject]) throws {
>
> property = try json.require("property")
> property1 = try json.require("property1")
> property2 = try json.require("property2")
> property3 = try json.require("property3")
>
> }
>
> }
>
> Require reads the key from the dict and tries to cast it to the same type as the variable its being assigned to and throws an error if this fails (like it doesn't exist or it can't be casted). This has much less boilerplate, its easier to read and also we can throw an error saying ("We couldn't parse property1").
>
> Some frameworks like Freddy JSON so something similar to this but with a much more complex model using Enums and it involves setting a bunch of parameters to tell the library when to throw a runtime error or not if it can't find something which if not handled correctly can cause a crash.
>
> We could make this even simpler yet if we could somehow treat casting as an error:
>
> class Model {
>
> let property: String
> let property1: String
> let property2: String
> let property3: String
>
> init?(json: [String: AnyObject]) throws {
>
> property = try json["property"] as String
> property1 = try json["property1"] as String
> property2 = try json["property2"] as String
> property3 = try json["property3"] as String
>
> }
>
> }
>
> If casting fails it throws an error, unfortunately we lose error information about the key so probably having a casting method like the one above is probable better. But in someways it explains what is happening.
>
>
>
>
> ___________________________________
>
> James⎥Head of Trolls
>
> james at supmenow.com <mailto:james at supmenow.com>⎥supmenow.com <http://supmenow.com/>
> Sup
>
> Runway East
>
>
> 10 Finsbury Square
>
> London
>
>
> EC2A 1AF
>
>
> On Thu, Mar 3, 2016 at 8:59 AM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> > Error handling forces you to do something about the possibility of a failure.
>
> So does using a failable initializer: the value is in an Optional, so you can't access it without either testing and unwrapping or just force-unwrapping. The type system, rather than the `try` requirement, is what forces you to handle the error, but it's the same idea.
>
> --
> Brent Royal-Gordon
> Architechies
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160303/e14feecd/attachment.html>
More information about the swift-evolution
mailing list