<div dir="ltr"><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><div class="gmail_extra"><div class="gmail_quote"><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><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><div> let someProperty: Any</div><div> let anotherProperty: Any</div><div><br></div><div> init?(data: [String: Any]) {</div><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"><div>let bar: String</div><div>if someCondition {</div><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>