<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<span>
<div align="left"><span><font face="Times New Roman"><span
style="font-size:11pt">For example, I have a function that
set some optional value:<br>
<br>
</span></font> <font color="#AA0D91" face="Times New
Roman"><span style="font-size:11pt">func</span></font> <font
face="Times New Roman"><span style="font-size:11pt">foo() {</span></font></span></div>
<div align="left"><font face="Times New Roman"><span
style="font-size:11pt"> </span></font> <font
color="#AA0D91" face="Times New Roman"><span
style="font-size:11pt">if</span></font> <font
color="#3F6E74" face="Times New Roman"><span
style="font-size:11pt">xxx</span></font> <font face="Times
New Roman"><span style="font-size:11pt">{</span></font></div>
<div align="left"><font face="Times New Roman"><span
style="font-size:11pt"> </span></font> <font
color="#AA0D91" face="Times New Roman"><span
style="font-size:11pt">switch</span></font> <font
color="#3F6E74" face="Times New Roman"><span
style="font-size:11pt">yyy</span></font> <font face="Times
New Roman"><span style="font-size:11pt">{</span></font></div>
<div align="left"><font face="Times New Roman"><span
style="font-size:11pt"> </span></font> <font
color="#AA0D91" face="Times New Roman"><span
style="font-size:11pt">case</span></font> <font face="Times
New Roman"><span style="font-size:11pt">.c1:</span></font></div>
<div align="left"><font face="Times New Roman"><span
style="font-size:11pt"> </span></font> <font
color="#3F6E74" face="Times New Roman"><span
style="font-size:11pt">someOptional</span></font> <font
face="Times New Roman"><span style="font-size:11pt">=</span></font>
<font color="#AA0D91" face="Times New Roman"><span
style="font-size:11pt">nil</span></font></div>
<div align="left"><font face="Times New Roman"><span
style="font-size:11pt"> </span></font> <font
color="#AA0D91" face="Times New Roman"><span
style="font-size:11pt">case</span></font> <font face="Times
New Roman"><span style="font-size:11pt">.c2:</span></font></div>
<div align="left"><font face="Times New Roman"><span
style="font-size:11pt"> </span></font> <font
color="#3F6E74" face="Times New Roman"><span
style="font-size:11pt">someOptional</span></font> <font
face="Times New Roman"><span style="font-size:11pt">=</span></font>
<font color="#1C00CF" face="Times New Roman"><span
style="font-size:11pt">5</span></font></div>
<div align="left"><font face="Times New Roman"><span
style="font-size:11pt"> }</span></font></div>
<div align="left"><font face="Times New Roman"><span
style="font-size:11pt"> }</span></font> <font
color="#AA0D91" face="Times New Roman"><span
style="font-size:11pt">else</span></font> <font face="Times
New Roman"><span style="font-size:11pt">{</span></font></div>
<div align="left"><font face="Times New Roman"><span
style="font-size:11pt"> </span></font> <font
color="#3F6E74" face="Times New Roman"><span
style="font-size:11pt">someOptional</span></font> <font
face="Times New Roman"><span style="font-size:11pt">=</span></font>
<font color="#AA0D91" face="Times New Roman"><span
style="font-size:11pt">nil</span></font></div>
<div align="left"><font face="Times New Roman"><span
style="font-size:11pt"> }</span></font></div>
<div align="left"><font face="Times New Roman"><span
style="font-size:11pt"> }</span></font></div>
<div align="left"><font face="Times New Roman"><span
style="font-size:11pt"><br>
Later I want to do something with the optional value. And I
know the optional is always not nil when xxx is satisfied
and yyy is in c2 case, so I just use force unwrapping. <br>
<br>
</span></font></div>
<div align="left"><font face="Times New Roman"><span
style="font-size:11pt"> </span></font> <font
color="#AA0D91" face="Times New Roman"><span
style="font-size:11pt">func</span></font> <font face="Times
New Roman"><span style="font-size:11pt">bar() {</span></font></div>
<div align="left"><font face="Times New Roman"><span
style="font-size:11pt"> </span></font> <font
color="#AA0D91" face="Times New Roman"><span
style="font-size:11pt">if</span></font> <font
color="#3F6E74" face="Times New Roman"><span
style="font-size:11pt">xxx</span></font> <font face="Times
New Roman"><span style="font-size:11pt">{</span></font></div>
<div align="left"><font face="Times New Roman"><span
style="font-size:11pt"> </span></font> <font
color="#AA0D91" face="Times New Roman"><span
style="font-size:11pt">switch</span></font> <font
color="#3F6E74" face="Times New Roman"><span
style="font-size:11pt">yyy</span></font> <font face="Times
New Roman"><span style="font-size:11pt">{</span></font></div>
<div align="left"><font face="Times New Roman"><span
style="font-size:11pt"> </span></font> <font
color="#AA0D91" face="Times New Roman"><span
style="font-size:11pt">case</span></font> <font face="Times
New Roman"><span style="font-size:11pt">.c1:</span></font></div>
<div align="left"><font face="Times New Roman"><span
style="font-size:11pt"> </span></font> <font
color="#26474B" face="Times New Roman"><span
style="font-size:11pt">doSomething1</span></font><font
face="Times New Roman"><span style="font-size:11pt">()</span></font></div>
<div align="left"><font face="Times New Roman"><span
style="font-size:11pt"> </span></font> <font
color="#AA0D91" face="Times New Roman"><span
style="font-size:11pt">case</span></font> <font face="Times
New Roman"><span style="font-size:11pt">.c2:</span></font></div>
<div align="left"><font face="Times New Roman"><span
style="font-size:11pt"> </span></font> <font
color="#26474B" face="Times New Roman"><span
style="font-size:11pt">doSomethingWithValue</span></font><font
face="Times New Roman"><span style="font-size:11pt">(</span></font><font
color="#3F6E74" face="Times New Roman"><span
style="font-size:11pt">someOptional</span></font> <font
face="Times New Roman"><span style="font-size:11pt">!)</span></font><font
color="#007400"><font face="Times New Roman"> </font></font><span><span><font
face="Times New Roman"><span style="font-size:11pt"><span
style="color: rgb(0, 116, 0);"></span></span></font><font
color="#007400" face="Times New Roman"><span
style="font-size:11pt">// force unwrapping, not very
good</span></font></span></span>
</div>
<div align="left"><font face="Times New Roman"><span
style="font-size:11pt"> }</span></font></div>
<div align="left"><font face="Times New Roman"><span
style="font-size:11pt"> }</span></font> <font
color="#AA0D91" face="Times New Roman"><span
style="font-size:11pt">else</span></font> <font face="Times
New Roman"><span style="font-size:11pt">{</span></font></div>
<div align="left"><font face="Times New Roman"><span
style="font-size:11pt"> </span></font> <font
color="#26474B" face="Times New Roman"><span
style="font-size:11pt">doSomething2</span></font><font
face="Times New Roman"><span style="font-size:11pt">()</span></font></div>
<div align="left"><font face="Times New Roman"><span
style="font-size:11pt"> }</span></font></div>
<div align="left"><font face="Times New Roman"><span
style="font-size:11pt"> }<br>
<br>
But this is not very good, since you can't tell from the
code why the optional is not nil, and if the function foo is
changed, you are not aware of the fact that the force
unwrapping is no longer valid. So is there some better
solution?<br>
<br>
</span></font></div>
</span>
</body>
</html>