<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">I've recently written a CoreData editor on iOS which automatically generates UI based on the model which is described using classes such as PrimitiveProperty, etc. Since it automatically sets the value on the entity, it needs to convert the value to AnyObject in order to pass it to setValue(_:forKey:), so it needs to be able to detect whether the value is Optional and in case it is, either transform the non-nil value to AnyObject (String -> NSString, Array -> NSArray, ...). Which is currently really hard to achieve: </div><div class=""><br class=""></div><div class=""><div class=""><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">var</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> obj: </span><span style="font-variant-ligatures: no-common-ligatures;" class="">IndexPath</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">? = </span><span style="font-variant-ligatures: no-common-ligatures;" class="">IndexPath</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">()</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> anyValue: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class="">Any</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);" class="">obj</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);" class="">anyValue</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">dynamicType</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">/// Optional<Foundation.IndexPath>.Type</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; min-height: 10px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""></span><br class=""></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">/// Using only anyValue, determine if it's Optional and retrieve its value if </span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">/// non-nil as AnyObject.</span></div></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">func</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> isOptional(anyValue: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class="">Any</span><span style="font-variant-ligatures: no-common-ligatures;" class="">) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class="">Bool</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> {</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="color: rgb(0, 132, 0);" class="">//</span><span style="color: rgb(0, 132, 0);" class=""> </span><span style="color: rgb(0, 132, 0);" class="">Error: Cannot downcast from 'Any' (aka 'protocol<>') to a more </span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="color: rgb(0, 132, 0);" class=""> //</span><span style="color: rgb(0, 132, 0);" class=""> </span><span style="color: rgb(0, 132, 0);" class="">optional type 'Optional<_>'</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> <font color="#bb2ca2" class="">return</font></span><span style="font-variant-ligatures: no-common-ligatures;" class=""> anyValue </span><span style="color: rgb(187, 44, 162); font-variant-ligatures: no-common-ligatures;" class="">is</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="color: rgb(112, 61, 170); font-variant-ligatures: no-common-ligatures;" class="">Optional</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="color: rgb(112, 61, 170); font-variant-ligatures: no-common-ligatures;" class=""><span style="color: rgb(0, 0, 0); font-variant-ligatures: no-common-ligatures;" class=""> </span></span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> anyValue </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">as</span><span style="font-variant-ligatures: no-common-ligatures;" class="">? </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class="">Optional</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> != </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">nil</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><font color="#bb2ca2" class=""> ...</font></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">}</span></div><div class=""><br class=""></div></div></span></div>Unless there are major reasons why it's not exposed, I'd propose introducing a new function isOptional(anyValue: Any) -> Bool, which would simply call Builtin.isOptional just like _isOptional does in Builtin.swift. (which pretty much is just taking the current _isOptional, removing underscore and marking it public).<div class=""><br class=""></div><div class="">However, this still doesn't help with the issue of retrieving the value of the Optional. You now know the value in `anyValue` is Optional, but there is no good way to cast it to e.g. Optional<AnyObject>. Here we're getting into a vicious cycle that Any can be an Optional which is Any.</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">My second part of the proposal introduces another function:</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> asOptional<T>(anyValue: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Any</span><span style="font-variant-ligatures: no-common-ligatures" class="">) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Optional</span><span style="font-variant-ligatures: no-common-ligatures" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">>?</span></div></div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">Which will:</div><div style="margin: 0px; line-height: normal;" class="">- return nil if !isOptional(anyValue)</div><div style="margin: 0px; line-height: normal;" class="">- return a non-nil value only if `anyValue` contains in fact an Optional of type T.</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">Usage:</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> anyObjOptional: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">AnyObject</span><span style="font-variant-ligatures: no-common-ligatures" class="">? = </span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">asOptional</span><span style="font-variant-ligatures: no-common-ligatures" class="">(anyValue: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">anyValue</span><span style="font-variant-ligatures: no-common-ligatures" class="">) {</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> anyObj = anyObjOptional {</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span> // anyObj is now the actual content of the optional.</div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span>}</div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">As a sidenote, this is my current workaround:</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">private</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">protocol</span><span style="font-variant-ligatures: no-common-ligatures" class=""> _XUOptional {</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> objectValue: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">AnyObject</span><span style="font-variant-ligatures: no-common-ligatures" class="">? { </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">get</span><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Optional</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">: </span><span style="font-variant-ligatures: no-common-ligatures" class="">_XUOptional</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><p style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; min-height: 10px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> objectValue: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class="">AnyObject</span><span style="font-variant-ligatures: no-common-ligatures;" class="">? {</span></p><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span> <span style="font-variant-ligatures: no-common-ligatures;" class="">switch</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">self</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> {</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class=""> </span> <span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> .</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(61, 29, 129);" class="">None</span><span style="font-variant-ligatures: no-common-ligatures;" class="">:</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span> <span style="font-variant-ligatures: no-common-ligatures;" class="">return</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">nil</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class=""> </span> <span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> .</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(61, 29, 129);" class="">Some</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">_</span><span style="font-variant-ligatures: no-common-ligatures;" class="">):</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span> <span style="font-variant-ligatures: no-common-ligatures;" class="">return</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">self</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">! </span><span style="font-variant-ligatures: no-common-ligatures;" class="">as</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">? </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class="">AnyObject</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span> }</div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> optional = anyValue </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">as</span><span style="font-variant-ligatures: no-common-ligatures" class="">? </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">_XUOptional</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> let object = optional.</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">objectValue</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class=""> /// ...</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div></span></div></div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div></body></html>