<div dir="ltr">I am trying to create an enum that will allow me to create literals that combine Int, Double and String types, as well as any Array of any combination of those, and any Dictionary mapping a String to any combination of those. And make it nestable.<div><br></div><div>First I have the enum itself, and then several examples. Everything complies cleanly except for dict4 and dict5.</div><div><br></div><div>Is type inference not expected to work for recursive enums? Or is this a bug in the compiler? If not is there any way to do this in pure swift that will compile?</div><div><br></div><div>I realize that it&#39;s sort of unswifty to have such unstructured, stringly typed data like this. But in a pinch sometimes it&#39;s useful.</div><div><br></div><div>Copy and paste this into a Playground to see the error message. (Contextual type &#39;protocol &lt;&gt;&#39; cannot be used with array literal and Contextual type &#39;protocol &lt;&gt;&#39; cannot be used with dictionary literal)</div><div><div><br></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:rgb(187,44,162)">enum</span><span style="color:rgb(0,0,0)"> RecursiveAny: </span>StringLiteralConvertible<span style="color:rgb(0,0,0)">, </span>ArrayLiteralConvertible<span style="color:rgb(0,0,0)">, </span>IntegerLiteralConvertible<span style="color:rgb(0,0,0)">, </span>BooleanLiteralConvertible<span style="color:rgb(0,0,0)">, </span>FloatLiteralConvertible<span style="color:rgb(0,0,0)">, </span>DictionaryLiteralConvertible<span style="color:rgb(0,0,0)"> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">case</span> any(Any)</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">indirect</span> <span style="color:rgb(187,44,162)">case</span> anyDict([String:Any])</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">indirect</span> <span style="color:rgb(187,44,162)">case</span> anyArr([Any])</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    </p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    </span>// string literal convertible</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">init</span>(stringLiteral value: <span style="color:rgb(112,61,170)">String</span>) {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">self</span> = .<span style="color:rgb(49,89,93)">any</span>(value)</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    </p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">init</span>(extendedGraphemeClusterLiteral value: <span style="color:rgb(112,61,170)">String</span>) {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">self</span> = .<span style="color:rgb(49,89,93)">any</span>(value)</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    </p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">init</span>(unicodeScalarLiteral value: <span style="color:rgb(112,61,170)">String</span>) {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">self</span> = .<span style="color:rgb(49,89,93)">any</span>(value)</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    </p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    </span>// array literal convertible</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">init</span>(arrayLiteral elements: <span style="color:rgb(112,61,170)">Any</span>...) {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">self</span> = .<span style="color:rgb(49,89,93)">anyArr</span>(elements)</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    </p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">init</span>(dictionaryLiteral elements: (<span style="color:rgb(112,61,170)">String</span>, <span style="color:rgb(112,61,170)">Any</span>)...) {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">var</span> dict = [<span style="color:rgb(112,61,170)">String</span>:<span style="color:rgb(112,61,170)">Any</span>]()</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">for</span> (key, value) <span style="color:rgb(187,44,162)">in</span> elements {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            dict[key] = value</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">self</span> = .<span style="color:rgb(49,89,93)">anyDict</span>(dict)</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    </p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    </span>// integer literal convertible</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">init</span>(integerLiteral value: <span style="color:rgb(112,61,170)">Int</span>) {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">self</span> = .<span style="color:rgb(49,89,93)">any</span>(value)</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    </p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    </span>// boolean literal convertible</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">init</span>(booleanLiteral value: <span style="color:rgb(112,61,170)">Bool</span>) {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">self</span> = .<span style="color:rgb(49,89,93)">any</span>(value)</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    </p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    </span>// float literal convertible</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">init</span>(floatLiteral value: <span style="color:rgb(112,61,170)">Double</span>) {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">self</span> = .<span style="color:rgb(49,89,93)">any</span>(value)</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span> string: <span style="color:rgb(79,129,135)">RecursiveAny</span> = <span style="color:rgb(209,47,27)">&quot;asdf&quot;</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(187,44,162)">let</span><span style="color:rgb(0,0,0)"> int: </span>RecursiveAny<span style="color:rgb(0,0,0)"> = </span><span style="color:rgb(39,42,216)">3</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(187,44,162)">let</span><span style="color:rgb(0,0,0)"> float: </span>RecursiveAny<span style="color:rgb(0,0,0)"> = </span><span style="color:rgb(39,42,216)">5.6</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span> array: <span style="color:rgb(79,129,135)">RecursiveAny</span> = [<span style="color:rgb(209,47,27)">&quot;asdf&quot;</span>, <span style="color:rgb(39,42,216)">3</span>, <span style="color:rgb(39,42,216)">5.6</span>]</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(187,44,162)">let</span><span style="color:rgb(0,0,0)"> dict: </span>RecursiveAny<span style="color:rgb(0,0,0)"> = [</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:rgb(0,0,0)">    </span>&quot;string&quot;<span style="color:rgb(0,0,0)">: </span>&quot;asdf&quot;<span style="color:rgb(0,0,0)">,</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(209,47,27)">&quot;int&quot;</span>: <span style="color:rgb(39,42,216)">3</span>,</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:rgb(0,0,0)">    </span>&quot;float&quot;<span style="color:rgb(0,0,0)">: </span><span style="color:rgb(39,42,216)">5.6</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">]</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span> dict2: <span style="color:rgb(79,129,135)">RecursiveAny</span> = [</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:rgb(0,0,0)">    </span>&quot;string&quot;<span style="color:rgb(0,0,0)">: </span>&quot;asdf&quot;<span style="color:rgb(0,0,0)">,</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(209,47,27)">&quot;int&quot;</span>: <span style="color:rgb(39,42,216)">3</span>,</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(209,47,27)">&quot;float&quot;</span>: <span style="color:rgb(39,42,216)">5.6</span>,</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:rgb(0,0,0)">    </span>&quot;array&quot;<span style="color:rgb(0,0,0)">: </span><span style="color:rgb(79,129,135)">array</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">]</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span> dict3: <span style="color:rgb(79,129,135)">RecursiveAny</span> = [</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:rgb(0,0,0)">    </span>&quot;string&quot;<span style="color:rgb(0,0,0)">: </span>&quot;asdf&quot;<span style="color:rgb(0,0,0)">,</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(209,47,27)">&quot;int&quot;</span>: <span style="color:rgb(39,42,216)">3</span>,</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(209,47,27)">&quot;float&quot;</span>: <span style="color:rgb(39,42,216)">5.6</span>,</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:rgb(0,0,0)">    </span>&quot;array&quot;<span style="color:rgb(0,0,0)">: </span><span style="color:rgb(79,129,135)">dict</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">]</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span> dict4: <span style="color:rgb(79,129,135)">RecursiveAny</span> = [</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:rgb(0,0,0)">    </span>&quot;string&quot;<span style="color:rgb(0,0,0)">: </span>&quot;asdf&quot;<span style="color:rgb(0,0,0)">,</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(209,47,27)">&quot;int&quot;</span>: <span style="color:rgb(39,42,216)">3</span>,</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(209,47,27)">&quot;float&quot;</span>: <span style="color:rgb(39,42,216)">5.6</span>,</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:rgb(0,0,0)">    </span>&quot;array&quot;<span style="color:rgb(0,0,0)">: [</span>&quot;asdf&quot;<span style="color:rgb(0,0,0)">, </span><span style="color:rgb(39,42,216)">3</span><span style="color:rgb(0,0,0)">, </span><span style="color:rgb(39,42,216)">5.6</span><span style="color:rgb(0,0,0)">]</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">]</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span> dict5: <span style="color:rgb(79,129,135)">RecursiveAny</span> = [</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:rgb(0,0,0)">    </span>&quot;string&quot;<span style="color:rgb(0,0,0)">: </span>&quot;asdf&quot;<span style="color:rgb(0,0,0)">,</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(209,47,27)">&quot;int&quot;</span>: <span style="color:rgb(39,42,216)">3</span>,</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(209,47,27)">&quot;float&quot;</span>: <span style="color:rgb(39,42,216)">5.6</span>,</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(209,47,27)">&quot;dict&quot;</span>: [</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:rgb(0,0,0)">        </span>&quot;string&quot;<span style="color:rgb(0,0,0)">: </span>&quot;asdf&quot;<span style="color:rgb(0,0,0)">,</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(209,47,27)">&quot;int&quot;</span>: <span style="color:rgb(39,42,216)">3</span>,</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(209,47,27)">&quot;float&quot;</span>: <span style="color:rgb(39,42,216)">5.6</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    ]</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">]</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span> dict6: <span style="color:rgb(79,129,135)">RecursiveAny</span> = [</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:rgb(0,0,0)">    </span>&quot;string&quot;<span style="color:rgb(0,0,0)">: </span>&quot;asdf&quot;<span style="color:rgb(0,0,0)">,</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(209,47,27)">&quot;int&quot;</span>: <span style="color:rgb(39,42,216)">3</span>,</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(209,47,27)">&quot;float&quot;</span>: <span style="color:rgb(39,42,216)">5.6</span>,</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(209,47,27)">&quot;array&quot;</span>: <span style="color:rgb(79,129,135)">RecursiveAny</span>.<span style="color:rgb(49,89,93)">anyArr</span>([<span style="color:rgb(209,47,27)">&quot;asdf&quot;</span>, <span style="color:rgb(39,42,216)">3</span>, <span style="color:rgb(39,42,216)">5.6</span>])</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">]</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span> dict7: <span style="color:rgb(79,129,135)">RecursiveAny</span> = [</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:rgb(0,0,0)">    </span>&quot;string&quot;<span style="color:rgb(0,0,0)">: </span>&quot;asdf&quot;<span style="color:rgb(0,0,0)">,</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(209,47,27)">&quot;int&quot;</span>: <span style="color:rgb(39,42,216)">3</span>,</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(209,47,27)">&quot;float&quot;</span>: <span style="color:rgb(39,42,216)">5.6</span>,</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(0,0,0)">    </span><span style="color:rgb(209,47,27)">&quot;dict&quot;</span><span style="color:rgb(0,0,0)">: </span>RecursiveAny<span style="color:rgb(0,0,0)">.</span><span style="color:rgb(49,89,93)">anyDict</span><span style="color:rgb(0,0,0)">([</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:rgb(0,0,0)">        </span>&quot;string&quot;<span style="color:rgb(0,0,0)">: </span>&quot;asdf&quot;<span style="color:rgb(0,0,0)">,</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(209,47,27)">&quot;int&quot;</span>: <span style="color:rgb(39,42,216)">3</span>,</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(209,47,27)">&quot;float&quot;</span>: <span style="color:rgb(39,42,216)">5.6</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    ])</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">]</p></div><div><br></div></div></div>