<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Swift already supports equivalent semantics to what you’re looking for, though conditional conformances are required for this to work with dictionaries and arrays too.</div><div class=""><br class=""></div><div class=""><div style="margin: 0px 0px 0px 33.7px; text-indent: -33.8px; line-height: normal; font-family: Menlo;" class=""><span style="color: #c900a4" class="">protocol</span> JSONValueType { }</div><div style="margin: 0px 0px 0px 33.7px; text-indent: -33.8px; line-height: normal; font-family: Menlo;" class=""><span style="color: #c900a4" class="">extension</span> <span style="color: #790ead" class="">String</span>: JSONValueType { }</div><div style="margin: 0px 0px 0px 33.7px; text-indent: -33.8px; line-height: normal; font-family: Menlo;" class=""><span style="color: #c900a4" class="">extension</span> <span style="color: #790ead" class="">Double</span>: JSONValueType { }</div><div style="margin: 0px 0px 0px 33.7px; text-indent: -33.8px; line-height: normal; font-family: Menlo;" class=""><span style="color: #c900a4" class="">extension</span> <span style="color: #790ead" class="">Int</span>: JSONValueType { }</div><div style="margin: 0px 0px 0px 33.7px; text-indent: -33.8px; line-height: normal; font-family: Menlo;" class=""><span style="color: #c900a4" class="">extension</span> <span style="color: #790ead" class="">Dictionary</span>: JSONValueType <span style="color: #c800a4" class="">where</span> Key <span style="color: #c800a4" class="">==</span> <span style="color: #790ead" class="">String</span>, Value: JSONValueType { }</div><div style="margin: 0px 0px 0px 33.7px; text-indent: -33.8px; line-height: normal; font-family: Menlo;" class=""><span style="color: #c900a4" class="">extension</span> <span style="color: #790ead" class="">Array</span>: JSONValueType <span style="color: #c800a4" class="">where</span> Element: JSONValueType { }</div><div style="margin: 0px 0px 0px 33.7px; text-indent: -33.8px; line-height: normal; font-family: Menlo;" class=""><span style="color: #c900a4" class="">extension</span> <span style="color: #790ead" class="">Bool</span>: JSONValueType { }</div><div style="margin: 0px 0px 0px 33.7px; text-indent: -33.8px; line-height: normal; font-family: Menlo; min-height: 16px;" class=""><br class=""></div><div style="margin: 0px 0px 0px 33.7px; text-indent: -33.8px; line-height: normal; font-family: Menlo;" class=""><span style="color: #c900a4" class="">let</span> data <span style="color: #c800a4" class="">=</span> [<span style="color: #790ead" class="">String</span> <span style="color: #c800a4" class="">:</span> JSONValueType]()</div><div style="margin: 0px 0px 0px 33.7px; text-indent: -33.8px; line-height: normal; font-family: Menlo;" class="">data[key] <span style="color: #c800a4" class="">=</span> <span style="color: #df0002" class="">"value"</span></div><div style="margin: 0px 0px 0px 33.7px; text-indent: -33.8px; line-height: normal; font-family: Menlo; min-height: 16px;" class=""><br class=""></div><div style="margin: 0px 0px 0px 33.7px; text-indent: -33.8px; line-height: normal; font-family: Menlo;" class=""><span style="color: #c900a4" class="">let</span> b: <span style="color: #790ead" class="">String</span>? <span style="color: #c800a4" class="">=</span> <span style="color: #c800a4" class="">nil</span></div><div style="margin: 0px 0px 0px 33.7px; text-indent: -33.8px; line-height: normal; font-family: Menlo; color: rgb(0, 142, 0);" class=""><span style="color: #c900a4" class="">let</span><span style="color: #000000" class=""> a: JSONValueType </span><span style="color: #c800a4" class="">=</span><span style="color: #000000" class=""> b </span>// error: value of optional type 'Optional&lt;string&gt;' not unwrapped</div></div><div class=""><br class=""></div><div class="">Cheers,</div><div class="">Jaden Geller</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Mar 12, 2017, at 7:52 PM, Elijah Johnson via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><span style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">I don’t know if this feature has yet existed in any languague, but its pretty simple.</span><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Let say you are preparing data for a JSON encoder. Right now, you would add to an Dictionary or Array like this one:</div><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">let data = [String:Any]();</div><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">but what one is really saying is that “Any” is from a number of distinct types:</div><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">group typedef JSONValueType : String, Double, Int, [String:JSONValueType], [JSONValueType], Bool</div><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">So from the compiler’s point of view, this group typedef would be just like the “Any” type, only it would refuse to compile unless the proper type was passed. So the result would be, as an example, that there could be created an (almost) fail-safe JSON encoder:</div><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">let data = [String:JSONValueType]();</div><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">data[“key] = “value”;</div><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">I’m sure this has plenty of uses outside of JSON encoding and would be pretty simple to do. One problem that it would solve it the fact that in Swift, everything is included by Any, including optionals, ex:</div><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-converted-space">&nbsp;</span>b :<span class="Apple-converted-space">&nbsp;</span></span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class="">String</span><span style="font-variant-ligatures: no-common-ligatures;" class="">? =<span class="Apple-converted-space">&nbsp;</span></span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">nil</span><span style="font-variant-ligatures: no-common-ligatures;" class="">;</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-converted-space">&nbsp;</span>a :<span class="Apple-converted-space">&nbsp;</span></span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">Any</span><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-converted-space">&nbsp;</span>= b; // result is warning: “Expression implicitly coerced from String? to Any"</span></div></div><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">and so there is no way to include value types and objects without also including optionals, though this “Any” (like Object and void* in Java/C) syntax makes for poorly documented code when the type isn’t really “Any”, but a collection of known types.</div><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">There already exist protocols, but they are not useful for types that one has not defined (like the basic system types), or for mixing particular values, structs, and classes.</div><span style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">_______________________________________________</span><br style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">swift-evolution mailing list</span><br style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="mailto:swift-evolution@swift.org" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">swift-evolution@swift.org</a><br style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div><br class=""></div></body></html>