<div dir="ltr">Catching and throwing exceptions is not the same as a controlled guard fail. It’s also quite verbose and requires extra types to be defined outside the scope of the initializer.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jul 25, 2017 at 5:35 PM, Rob Mayoff 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 dir="ltr"><span class=""><div class="gmail_extra"><div class="gmail_quote">On Tue, Jul 25, 2017 at 4:44 AM, philohan95 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:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">As you can see we had to use the properties twice (this would also be the case of `if let`) making the initializer twice as long as necessary and becomes a pain to implement when having more than 1 property.<br></blockquote></div></div></span><div class="gmail_extra"><div class="gmail_quote"><span class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
My idea is extending the power of the `guard` statement<br>
<br>
Idea:<br>
init?(data: [String: Any]) {<br>
guard<br>
someProperty = data["some_key"], // Currently fails because `self` us used before all stored properties are initialized<br>
anotherProperty = data["another_key"]<br>
else {<br>
return nil<br>
}<br>
}<br>
}<br></blockquote><div><br></div></span><div>I'm not convinced new syntax is necessary. You can get pretty close to this today. First, put this in your project somewhere:</div><div><br></div></div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_quote"><div><div>struct NilError: Error { }</div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><br></div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div>func throwNilError<Whatever>() throws -> Whatever {</div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div> throw NilError()</div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div>}</div></div><div><br></div></div></div></blockquote><div class="gmail_extra"><div class="gmail_quote"><div>Then use do/try/catch to initialize your properties:</div><div><div><br></div></div></div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_quote"><div><div><div>class MyObject {</div><span class=""><div> let someProperty: Any</div><div> let anotherProperty: Any</div><div><br></div><div> init?(data: [String: Any]) {</div></span><div> do {</div><div> someProperty = try data["some_key"] ?? throwNilError()</div><div> anotherProperty = try data["another_key"] ?? throwNilError()</div><div> } catch {</div><div> return nil</div><div> }</div><div> }</div><div>}</div></div></div><div><br></div></div></div></blockquote>It also works for Charles Srstka's example:<div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><span class=""><div>let bar: String</div><div>if someCondition {</div></span><div> do { bar = mightReturnOptional() ?? throwNilError() }</div><div> catch { return }</div><div>} else {</div><div> bar = wontReturnOptional()</div><div>}</div></blockquote><div><div><br></div><div><br><div class="gmail_extra"><div class="gmail_quote"><div><br></div></div></div></div></div></div>
<br>______________________________<wbr>_________________<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/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
<br></blockquote></div><br></div>