<div dir="ltr">Wouldn&#39;t a struct be the better answer here overall or (with slightly fewer assurances) using an enum as the `Value` type?<div><br><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jun 10, 2017 at 12:56 PM, David Moore via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div id="m_-2813548072538771508edo-message"><div><br></div></div><div id="m_-2813548072538771508edo-original"><div><blockquote type="cite" style="margin:1ex 0 0 0;border-left:1px #ccc solid;padding-left:0.5ex"><div>



<div name="messageBodySection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif">Hello swift-evolution,
<div><br></div><div>There are clearly weaknesses with obtaining values from a given dictionary in swift. Most notably, if we have a [String: Any] dictionary, how is one supposed to set/get a value with type safety? The following concept addresses this very issue.</div><div><br></div>
<div>I regularly use a self-constructed structure with static constants embedded within it, the structure enables a type safe implementation of keys and values (especially Dictionary applications). I will include a code snippet later on in this message, but I would specifically like to describe the benefits of a type safe key and value system, and how it would benefit the language. </div>
<div><br></div>
<div>When using dictionaries in Swift, one often finds themselves needing to use more general key and value types (e.g. [String: Any]). Unfortunately, when a Dictionary uses a general type, rather than one with more type safety and compile time checks, many issues can arise from such a use case.</div>
<div><br></div>
<div>The solution, in my opinion, is to use a pairing struct which can be used to create constant type-safe-enforcing structures. Additionally, said struct could have the ability to transform values of one type to that of another. Please see the code snippet below for more information regarding the implementation.</div>
<div><br></div>
<blockquote style="margin:5px 5px;padding-left:10px;border-left:thin solid #1abc9c">
<div>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(225,45,160);background-color:rgb(41,43,54)">struct <span style="color:#e7e8eb">Bar {</span></p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,232,235);background-color:rgb(41,43,54)">    <span style="color:#e12da0">var</span> str: <span style="color:#29a09f">String</span></p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,232,235);background-color:rgb(41,43,54)">}</p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Helvetica;background-color:rgb(41,43,54);min-height:14px"><br></p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,232,235);background-color:rgb(41,43,54)"><span style="color:#e12da0">var</span> foo = [<span style="color:#29a09f">String</span>: <span style="color:#e12da0">Any</span>]()</p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(222,58,60);background-color:rgb(41,43,54)"><span style="color:#18b5b1">foo</span><span style="color:#e7e8eb">[</span>&quot;bar&quot;<span style="color:#e7e8eb">] =</span> <span style="color:#18b5b1">Bar</span><span style="color:#e7e8eb">(str:</span> &quot;Hello, world.&quot;<span style="color:#e7e8eb">)</span></p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Helvetica;background-color:rgb(41,43,54);min-height:14px"><br></p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(81,195,79);background-color:rgb(41,43,54)">// We are optionally casting this value of `Any` to `Bar`, but it&#39;s strongly typed, so there are clearly drawbacks.</p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,232,235);background-color:rgb(41,43,54)"><span style="color:#e12da0">if</span> <span style="color:#e12da0">let</span> bar = <span style="color:#18b5b1">foo</span>[<span style="color:#de3a3c">&quot;bar&quot;</span>] <span style="color:#e12da0">as</span>? <span style="color:#18b5b1">Bar</span> {</p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,232,235);background-color:rgb(41,43,54)">    bar.<span style="color:#18b5b1">str</span></p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,232,235);background-color:rgb(41,43,54)">}</p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Helvetica;background-color:rgb(41,43,54);min-height:14px"><br></p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(81,195,79);background-color:rgb(41,43,54)">// What if we use a `KeyValuePair`?</p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,232,235);background-color:rgb(41,43,54)"><span style="color:#e12da0">let</span> barPair = <span style="text-decoration:underline">K</span>eyValuePair&lt;<span style="color:#29a09f">String</span>, <span style="color:#e12da0">Any</span>, <span style="color:#18b5b1">Bar</span>&gt;(key: <span style="color:#de3a3c">&quot;bar&quot;</span>)</p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,232,235);background-color:rgb(41,43,54)"><span style="color:#18b5b1">barPair</span>.addValue(<span style="color:#18b5b1">Bar</span>(str: <span style="color:#de3a3c">&quot;This is better.&quot;</span>), to: &amp;<span style="color:#18b5b1">foo</span>)</p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Helvetica;background-color:rgb(41,43,54);min-height:14px"><br></p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(81,195,79);background-color:rgb(41,43,54)">// Now look how easy (and safe) it is to get the value.</p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,232,235);background-color:rgb(41,43,54)"><span style="color:#e12da0">let</span> bar = <span style="color:#18b5b1">barPair</span>.value(from: <span style="color:#18b5b1">foo</span>)</p>
</div>
</blockquote>
<font color="#E7E8EB" face="Menlo" size="2"><br></font>
<div>I have already implemented the underlying structure for the above example usage. Also, there are other use cases that are not highlighted here, such as built in value transformation. <br></div>
<div><br></div>
<div>Let me know if anyone else agrees that this is a type safe solution for generalized dictionaries, or if think there are other uses for it. I also have all the code for the struct KeyValuePair, if anyone wants to see how it works, but it’s quite lightweight.</div>
<div><br></div>
<div>Thank you,</div>
</div>
<div name="messageSignatureSection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif"><br>
<div>
<p style="font-family:Helvetica,Arial,sans-serif;font-size:10px;line-height:12px;margin-bottom:10px"><a style="text-decoration:none"><img src="https://www.mooredev.ca/logo/Logo.png" alt="Moore Development" border="0" height="80" width="102"></a></p>
<p style="font-family:Helvetica,Arial,sans-serif;font-size:10px;line-height:12px;color:rgb(33,33,33);margin-bottom:10px"><span style="font-weight:bold;color:rgb(33,33,33);display:inline">David Moore</span> <span style="display:inline">/</span> <span style="color:rgb(33,33,33);display:inline">Owner / Chief Executive Officer</span> <span style="display:inline"><br></span> <a href="mailto:david@mooredev.ca" style="color:rgb(71,124,204);text-decoration:none;display:inline" target="_blank">david@mooredev.ca</a> <span style="display:inline">/</span> <span style="color:rgb(33,33,33);display:inline"><a href="tel:(705)%20845-6057" value="+17058456057" target="_blank">(705) 845-6057</a></span></p>
<p style="font-family:Helvetica,Arial,sans-serif;font-size:10px;line-height:12px;margin-bottom:10px"><span style="font-weight:bold;color:rgb(33,33,33);display:inline">Moore Development</span> <span style="display:inline"><br></span> <a href="https://www.mooredev.ca" style="color:rgb(71,124,204);text-decoration:none;display:inline" target="_blank">https://www.mooredev.ca</a></p>
<p style="font-size:0px;line-height:0;font-family:Helvetica,Arial,sans-serif"></p>
<p style="font-family:Helvetica,Arial,sans-serif;font-size:10px;line-height:12px;margin-bottom:10px"><a href="https://www.mooredev.ca/" target="_blank"></a></p>
<p style="font-family:Helvetica,Arial,sans-serif;color:#212121;font-size:9px;line-height:12px"></p>
</div>
</div>


</div></blockquote></div></div></div><br>______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
<br></blockquote></div><br></div>