<div dir="ltr"><div>Clarifying questions as comments:</div><div><br></div><div><div class="gmail_extra"><div class="gmail_extra"> guard device = MTLCreateSystemDefaultDevice() else {</div><div class="gmail_extra"> fatalError()</div><div class="gmail_extra"> }</div><div class="gmail_extra"> device = nil <b>// Is this still allowed here?</b></div><div class="gmail_extra"> device.newDefaultLibrary() <b>// Is this allowed without optional chaining?</b></div><div><br></div><div>Also, I don't see any reason this idea should be restricted to instance variables. It could work for any variable, I think, but in any case it gets tricky if the value is mutated again afterwards.</div><div><br></div><div><div><div dir="ltr"><div>Jacob<br></div></div></div></div>
<br><div class="gmail_quote">On Mon, Apr 11, 2016 at 12:31 AM, Taras Zakharko 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-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">One place where I find myself using the guard statement a lot is state initialisation. E.g. in init()<br>
<br>
guard let<br>
device = MTLCreateSystemDefaultDevice(),<br>
library = device.newDefaultLibrary()<br>
else {<br>
fatalError(“Could not initiallize Metal, aborting”)<br>
}<br>
<br>
Here, the two variables device and library are intended to be instance variables of an engine backbone system. However, because optional binding in guard can only introduce new local statements, one needs to add additional boilerplate in the end, e.g.:<br>
<br>
self.device = device<br>
self.library = library<br>
<br>
What I want to propose is a very simple QOL enhancement: allow optional binding to be used with instance variables in the constructors. If the binding fails, the instance construction cannot proceed (the relevant scope is the instance). The syntax would be<br>
<br>
guard device = MTLCreateSystemDefaultDevice(),<br>
library = device.newDefaultLibrary()<br>
else {<br>
fatalError(“Could not initiallize Metal, aborting”) // or return nil<br>
}<br>
<br>
<br>
Few notes:<br>
<br>
- This would only apply to guard statement, because binding in the if statement only applies to the internal if scope by definition<br>
- One could also ask for instance optional binding in any context where instance assignment is legal. However, this would make guard inconsistent, as it won’t be able to prevent inconsistent state from being invisible to the app. Therefore its best to restrict this to cases where state is being constructed, and fail the construction if the guard fails (hence init() )<br>
<br>
Best,<br>
<br>
Taras<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
_______________________________________________<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" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div><br></div></div></div>