<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
</head>
<body dir="ltr">
<div id="divtagdefaultwrapper" style="font-size:12pt;color:#000000;background-color:#FFFFFF;font-family:Calibri,Arial,Helvetica,sans-serif;">
<p><span style="font-size: 12pt;">Extension is a powerful feature in Swift, allowing type extensibility in various way. However, there are some limitations that everyone has to cope with. W</span><span style="font-size: 12pt;">e cannot add
</span><span style="font-size: 12pt;">stored variables to </span><font size="3">extension scope. ( enclosed </font>variable <font size="3">)</font><br>
</p>
<p><span style="font-size: 12pt;"><br>
</span></p>
<p>To workaround with the problem, associated object <font size="3">has been used so far by m</font><span style="font-size: medium;">any Swift/Obj-c library that we are using today. This technique facilitates the power of extension. </span><span style="font-size: medium;">It
allows extension to have enclosed stored property at runtime. </span><font size="3">However, we can only apply this method to NSObject subclass type. And I think it is some kind of hack, and </font><font size="3">when it comes to Swift the syntax looks messy.</font></p>
<p><font size="3"><br>
</font></p>
<p>So, I would like to propose an idea of storing variable inside extension scope. '<strong>enclose</strong>', is a prefix keyword, stating that a variable is only visible inside an extension block scope.<font size="3"><br>
</font></p>
<p><br>
</p>
<p><br>
</p>
<table cellspacing="0" width="100%" role="table" class="ms-rteTable-default" style="border-collapse:collapse; border: 1px solid rgb(198, 198, 198);">
<tbody>
<tr>
<td class="ms-rteTable-default" style="border-collapse: collapse; border: 1px solid rgb(198, 198, 198); width: 100%;">
​<span style="font-family: Menlo; font-size: 12px; color: rgb(187, 44, 162);">protocol</span><span style="font-family: Menlo; font-size: 12px;"> </span><span style="color: rgb(79, 129, 135); font-family: Menlo; font-size: 12px;">Incrementor</span><span style="font-family: Menlo; font-size: 12px;">{</span>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo;">
<span style="color: rgb(187, 44, 162);">func</span> increase()<br>
</p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo;">
<span style="color: rgb(187, 44, 162);">func</span> otherFunc()</p>
<p></p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo;">
}<br>
</p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo;">
<br>
</p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);">
<span style="color: rgb(187, 44, 162);">extension</span><span style="color: rgb(0, 0, 0);"> </span>Incrementor<span style="color: rgb(0, 0, 0);">{</span></p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo; min-height: 14px;">
</p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo;">
<span style="color: rgb(0, 132, 0);">/* </span></p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);">
enclose keyword only allow to be used in extension scope</p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);">
*/<br>
</p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);">
<span style="color: rgb(255, 0, 0);"> <span style="color: rgb(255, 0, 0);">enclose</span> </span><span style="color: rgb(187, 44, 162);">var</span><span style="color: rgb(0, 0, 0);"> count = </span><span style="color: rgb(39, 42, 216);">1</span><span style="color: rgb(0, 0, 0);"> </span>//
count is visible only in this extension scope.</p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo; min-height: 14px;">
</p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo;">
<span style="color: rgb(187, 44, 162);">func</span> increase(){<br>
</p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo;">
<span style="color: rgb(61, 29, 129);">print</span>(count)<br>
</p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo;">
count = count + <span style="color: rgb(39, 42, 216);">1</span></p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo;">
}<br>
</p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo;">
</p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo;">
}<br>
</p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo;">
<br>
</p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);">
// another extension scope</p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);">
<span style="color: rgb(187, 44, 162);">extension</span><span style="color: rgb(0, 0, 0);"> </span>Incrementor<span style="color: rgb(0, 0, 0);">{</span></p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo; min-height: 14px;">
</p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);">
<span style="color: rgb(0, 0, 0);"> </span>// can't see 'count' because it's in another extension scope.</p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo;">
<span style="color: rgb(187, 44, 162);">func</span> otherFunc(){</p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);">
<span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(61, 29, 129);">print</span><span style="color: rgb(0, 0, 0);">(</span>"do whatever but you will not see 'count' in this"<span style="color: rgb(0, 0, 0);">)</span></p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo;">
}</p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo;">
</p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo;">
}<br>
</p>
</td>
</tr>
</tbody>
</table>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo;">
<br>
</p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo;">
<br>
</p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo;">
<span style="font-family: Calibri, Arial, Helvetica, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', NotoColorEmoji, 'Segoe UI Symbol', 'Android Emoji', EmojiSymbols; font-size: 12pt;">This allows mixins composition style. </span><span style="font-family: Calibri, Arial, Helvetica, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', NotoColorEmoji, 'Segoe UI Symbol', 'Android Emoji', EmojiSymbols; font-size: 12pt;">It
has no side-effect to other parts, and is considered to have better separation of concerns. </span><span style="font-family: Calibri, Arial, Helvetica, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', NotoColorEmoji, 'Segoe UI Symbol', 'Android Emoji', EmojiSymbols; font-size: 12pt;">Since
we have protocol extension in Swift 2.0, this feature will empower composition scheme, and eliminates associated object hacking from Swift/Obj-c. </span></p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo;">
<span style="font-family: Calibri, Arial, Helvetica, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', NotoColorEmoji, 'Segoe UI Symbol', 'Android Emoji', EmojiSymbols; font-size: 12pt;"><br>
</span></p>
<p style="margin-right: 0px; margin-left: 0px; line-height: normal;"><font size="3">Think about rewriting massive view controller as extensions composition. We don't have to store all states of the view controller class in one place. We can </font>separate<font size="3"> it
to multiple part, each part consists of it own states using enclose keyword.</font></p>
<p style="margin-right: 0px; margin-left: 0px; font-size: 12px; line-height: normal; font-family: Menlo;">
<span style="font-family: Calibri, Arial, Helvetica, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', NotoColorEmoji, 'Segoe UI Symbol', 'Android Emoji', EmojiSymbols; font-size: 12pt;"><br>
</span></p>
<p style="margin-right: 0px; margin-left: 0px; line-height: normal;"><font size="3">I'm pretty sure it will be very useful. So, I would like to ask the opinions of you guys about pros and cons of doing this. What might be an alternative solution</font>? <font size="3">Is
it consistent ?</font></p>
</div>
</body>
</html>