<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel>
        <title>Daniel Duan's Articles About OOP</title>
        <link>https://duan.ca/tag/oop/</link>
        <atom:link href="https://duan.ca/tag/oop/feed.xml" rel="self" type="application/rss+xml" />
            <item>
                <title>A Case For OOP?</title>
                <description>&#60;p&#62;Python&#39;s standard library includes a very handy &#60;code&#62;defaultdict&#60;/code&#62;. It behaves
almost exactly like the standard dictionary except it&#39;ll supply a pre-defined
value for any non-existence keys. It is, unsurpringly, a subclass of &#60;code&#62;dict&#60;/code&#62;.&#60;/p&#62;
&#60;p&#62;I find my self missing this handy container in Swift. Especially when I use
a normal &#60;code&#62;Dictionary&#60;/code&#62; to accumulate/coalesce values under distinct keys. So I
wrote my own:&#60;/p&#62;
&#60;script src=&#34;https://gist.github.com/dduan/31ed39c4c98ecb88290f0743cb394c20.js&#34;&#62;&#60;/script&#62;
&#60;p&#62;There are a few noticable things about this implementation:&#60;/p&#62;
&#60;ul&#62;
&#60;li&#62;It does not conform to the &#60;code&#62;DictionaryLiteralConvertible&#60;/code&#62; protocol, for no
good reasons, really. The initializer in this protocol takes a varadic
argument. There&#39;s no conevient way to forward this array to a normal
dictionary&#39;s initializer (incidentally, this is a Swift feature I really
want). Plus, I don&#39;t need &#60;code&#62;DefaultDictionary&#60;/code&#62; to be a literal convertible.&#60;/li&#62;
&#60;li&#62;Most of the code, including the imaginary &#60;code&#62;init&#60;/code&#62; mentioned in previous
point, simply reuses stuff from &#60;code&#62;Dictionary&#60;/code&#62;: asscociated type, indexes,
generator, subscript, etc.&#60;/li&#62;
&#60;/ul&#62;
&#60;p&#62;In comparison, Python implements &#60;code&#62;defaultdict&#60;/code&#62; in a more intuitive way -- via
inheritance.&#60;/p&#62;
&#60;p&#62;But do we want inheritance for &#60;code&#62;struct&#60;/code&#62;s and &#60;code&#62;enum&#60;/code&#62;s in Swift? What does that
even mean? Is it simply a case that will go away when protocols become more
powerful?&#60;/p&#62;
</description>
                <pubDate>Thu, 19 May 2016 12:03:02 -0700</pubDate>
                <link>https://duan.ca/2016/05/19/a-case-for-oop/</link>
                <guid isPermaLink="true">https://duan.ca/2016/05/19/a-case-for-oop/</guid>
            </item>
    </channel>
</rss>