<div dir="ltr">Not that I personally use failable initializers, but aren't they needed to interact with Obj-C which returns nil to indicate failure from initializers?</div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"> -- Howard.<br></div></div>
<br><div class="gmail_quote">On 3 March 2016 at 09:27, Haravikk via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>I haven’t used a single failable initialiser ever since the try! command was added, as that gave the same convenience as force unwrapping and eliminated the need for big try/catch blocks if you didn’t expect an exception, with try? likewise making it easy to detect that any exception occurred, without caring about what it was.</div><div><br></div><div>So I’d probably say that failable initialisers are unnecessary now; they’re a tiny bit cleaner for simpler failable initialisers (one type of failure), but it’s probably not worth keeping as a language feature when it comes up so rarely (at least in my experience).</div><br><div><blockquote type="cite"><div>On 2 Mar 2016, at 21:11, James Campbell via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br><div><div dir="ltr">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?<div><br></div><div>Take this model</div><div><br></div><div>struct MyModel {</div><div><br></div><div>let someProperty</div><div>let anotherProperty</div><div>}</div><div><br></div><div>Lets say we have a Initializers for this structure that takes a JSON structure and returns an i.</div><div><br></div><div>We could guarantee that it always returns an instance but then we have to populate this data with dummy values if the JSON is missing values.</div><div><br></div><div>I could make these properties optional to reflect that the JSON was missing information but I would be making the structure mutable which adds complexity. On-top of that I wouldn't be able to tell if the property is nil due to a lack of a value or a bug causing certain JSON information to be missing.</div><div><br></div><div>So lets look at the alternatives with a non-guranteed system, well we have Failable Initializers. If any of the properties are missing, we should return nil.</div><div><br></div><div>Well we have this issue:</div><div><br></div><div>MyModel(json) </div><div><br></div><div>It isn't clear that this is fallable compared to a traditional initializer unless swift was update to, meaning we may forget to handle the nil case:</div><div><br></div><div> <b>MyModel?(json)</b></div><div><b><br></b></div><div>This isn't all that is wrong with this approach, if this method does a lot of steps to create the object and fails (Like parsing an object from JSON). Returning nil but we have no idea why, it makes it easy to introduce silently failing errors. </div><div><br></div><div>Its true some classes may do this when ran on a older version of iOS but with avaliabilty checks, this use case is irrelevant. </div><div><b><br></b></div><div>So what about throwing an error?</div><div><br></div><div>It forces us to handle it failing to initialize for some reason. </div><div><br></div><div>- If we forget to handle the error, the app won't be allowed to continue with this corrupted data - and the error will be informative as it forces us to create an ErrorType.</div><div>- If we want to ignore this object and convert the error to nil we can still use <b>try? </b>(For example in a Chat application you could ignore corrupted messages).</div><div>- In these cases where we ignore the error using <b>try?</b> it makes it super obvious we are ignoring an error and it should be easy to that error causing it to fail by using <b>try! </b></div><div>- On the whole it encourages us to not return nil and to return useful error codes which we can handle, what if one of the reasons it failed to parse is something we could recover from? If we return nil we will never know.</div><div>- And it reduces the amount of mutability in swift.</div><div><br></div><div><br></div><div><div><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><p><b><font color="#cc0000">___________________________________</font></b></p><p><b>James⎥Head of Trolls</b></p><p><b><font color="#cc0000"><a href="mailto:james@supmenow.com" target="_blank">james@supmenow.com</a>⎥<a href="http://supmenow.com/" target="_blank">supmenow.com</a></font></b></p><p><b><font size="2">Sup</font></b></p><p><b><font size="2">Runway East
</font></b></p><p><b><font size="2">10 Finsbury Square</font></b></p><p><b><font size="2">London</font></b></p><p><b><font size="2">
EC2A 1AF </font></b></p></div></div></div></div></div></div></div></div></div></div></div>
</div></div>
_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div></blockquote></div><br></div><br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>