<div dir="ltr">I’d be in favor of this.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jul 25, 2017 at 5: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:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I think the current way to initiate models in a Failable Initializer `init?()` is overly verbose and should be shortened down so less boilerplate should be needed.<br>
<br>
The current way:<br>
<br>
```<br>
let someProperty: Any<br>
let anotherProperty: Any<br>
<br>
init?(data: [String: Any]) {<br>
guard<br>
let someProperty = data["some_key"],<br>
let anotherProperty = data["another_key"]<br>
else {<br>
return nil<br>
}<br>
<br>
self. someProperty = someProperty<br>
self. anotherProperty = anotherProperty<br>
}<br>
```<br>
<br>
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>
<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>
<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>
</blockquote></div><br></div>