Daniel Duan

A Case For OOP?

Python’s standard library includes a very handy defaultdict. It behaves almost exactly like the standard dictionary except it’ll supply a pre-defined value for any non-existence keys. It is, unsurpringly, a subclass of dict.

I find my self missing this handy container in Swift. Especially when I use a normal Dictionary to accumulate/coalesce values under distinct keys. So I wrote my own:

There are a few noticable things about this implementation:

In comparison, Python implements defaultdict in a more intuitive way – via inheritance.

But do we want inheritance for structs and enums in Swift? What does that even mean? Is it simply a case that will go away when protocols become more powerful?