<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">On Mar 3, 2016, at 1:09 AM, James Campbell via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><div><blockquote type="cite" class=""><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">True but in my example above for parsing a JSON into a model I can do this:<div class=""><br class=""></div><div class="">class Model {</div><div class=""><br class=""></div><div class="">let property: String</div><div class="">let property1: String<br class=""></div><div class="">let property2: String<br class=""></div><div class="">let property3: String<br class=""></div><div class=""><br class=""></div><div class="">init?(json: [String: AnyObject]) {</div><div class=""><br class=""></div><div class="">guard property = json["property"] as? String else {</div><div class=""> return nil</div><div class="">}</div><div class=""><br class=""></div><div class=""><div class="">guard property = json["property1"] as? String else {</div><div class=""> return nil</div><div class="">}</div></div><div class=""><br class=""></div><div class=""><div class="">guard property = json["property2"] as? String else {</div><div class=""> return nil</div><div class="">}</div></div><div class=""><br class=""></div><div class=""><div class="">guard property = json["property3"] as? String else {</div><div class=""> return nil</div><div class="">}</div></div></div></div></blockquote><div><br class=""></div><div>Just FYI, but you can of course write this as:</div><div><br class=""></div><div>guard let property = json["property"] as? String,<br class=""> let property2 = json["property1"] as? String,<br class=""> let property3 = json["property2"] as? String,<br class=""> let property4 = json["property3"] as? String else {<br class=""> return nil<br class="">}</div><div><br class=""></div><div>-Chris<br class=""><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><br class=""></div><div class="">}</div><div class=""><br class=""></div><div class="">}</div><div class=""><br class=""></div><div class="">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. </div><div class=""><br class=""></div><div class="">Compare this to error handling:</div><div class=""><br class=""></div><div class=""><div class="">class Model {</div><div class=""><br class=""></div><div class="">let property: String</div><div class="">let property1: String<br class=""></div><div class="">let property2: String<br class=""></div><div class="">let property3: String<br class=""></div><div class=""><br class=""></div><div class="">init?(json: [String: AnyObject]) throws {</div><div class=""><br class=""></div><div class="">property = try json.require("property")</div><div class="">property1 = try json.require("property1")</div><div class="">property2 = try json.require("property2")<br class=""></div><div class="">property3 = try json.require("property3")<br class=""></div><div class=""><br class=""></div><div class="">}</div><div class=""><br class=""></div><div class="">}</div></div><div class=""><br class=""></div><div class="">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"). </div><div class=""><br class=""></div><div class="">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.</div><div class=""><br class=""></div><div class="">We could make this even simpler yet if we could somehow treat casting as an error:</div><div class=""><br class=""></div><div class=""><div class="">class Model {</div><div class=""><br class=""></div><div class="">let property: String</div><div class="">let property1: String<br class=""></div><div class="">let property2: String<br class=""></div><div class="">let property3: String<br class=""></div><div class=""><br class=""></div><div class="">init?(json: [String: AnyObject]) throws {</div><div class=""><br class=""></div><div class="">property = try json["property"] as String</div><div class="">property1 = try json["property1"] as String</div><div class="">property2 = try json["property2"] as String<br class=""></div><div class="">property3 = try json["property3"] as String<br class=""></div><div class=""><br class=""></div><div class="">}</div><div class=""><br class=""></div><div class="">}</div></div><div class=""><br class=""></div><div class="">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.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div></div><div class="gmail_extra"><br clear="all" class=""><div class=""><div class="gmail_signature"><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><p class=""><b class=""><font color="#cc0000" class="">___________________________________</font></b></p><p class=""><b class="">James⎥Head of Trolls</b></p><p class=""><b class=""><font color="#cc0000" class=""><a href="mailto:james@supmenow.com" target="_blank" class="">james@supmenow.com</a>⎥<a href="http://supmenow.com/" target="_blank" class="">supmenow.com</a></font></b></p><p class=""><b class=""><font size="2" class="">Sup</font></b></p><p class=""><b class=""><font size="2" class="">Runway East
</font></b></p><p class=""><b class=""><font size="2" class="">10 Finsbury Square</font></b></p><p class=""><b class=""><font size="2" class="">London</font></b></p><p class=""><b class=""><font size="2" class="">
EC2A 1AF </font></b></p></div></div></div></div></div></div></div></div></div></div></div>
<br class=""><div class="gmail_quote">On Thu, Mar 3, 2016 at 8:59 AM, Brent Royal-Gordon via swift-evolution <span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">> Error handling forces you to do something about the possibility of a failure.<br class="">
<br class="">
</span>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.<br class="">
<span class="HOEnZb"><font color="#888888" class=""><br class="">
--<br class="">
Brent Royal-Gordon<br class="">
Architechies<br class="">
</font></span><div class="HOEnZb"><div class="h5"><br class="">
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</div></div></blockquote></div><br class=""></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>