A recent question on programmers.stackexchange.com asked What functionality does dynamic typing allow?

I thought one of the best short answers to this was from Mark Ransom:

Theoretically there’s nothing you can’t do in either, as long as the languages are Turing Complete. The more interesting question to me is what’s easy or natural in one vs. the other.

This post is about providing an example to back that up, and to respond to people who claim that, since you can implement dynamic types in a statically typed language, statically typed languages give you all the benefits of dynamically typed languages.

[Edit: to those who think I’m being a language or dynamic typing advocate or engaging in any kind of bashing, please read that last paragraph again, and note especially the use of word ‘all’.]

Let’s set up a problem. It’s made up, but it illustrates the point I want to make:

Given a file, ‘invoices.yaml’, take the first document in it, extract the ‘bill-to’ field, and save the data in it as JSON in an output file ‘address.json’. You can take it for granted that the contents of that field can be serialised as JSON (e.g. doesn’t contain dates), although that might not be true for the rest of the document. To keep the example focussed and simple, everything will be ASCII .

The particular YAML file I used was taken from an example YAML document I found on the web, and then expanded for the sake of illustration:

--- invoice: 34843 date : 2001-01-23 bill-to: given : Chris family : Dumars address: lines: | 458 Walkman Dr. Suite #292 city : Royal Oak state : MI postal : 48046 --- invoice: 34844 date : 2001-01-24 bill-to: given : Pete family : Smith address: lines: | 3 Amian Rd city : Royal Oak state : MI postal : 48047

I’ll use Python and Haskell as representatives of dynamic typing and static typing, because I know them and many would consider them to be very good representatives of their camps, and I’m a big fan of both languages.

I also think that examining any programming problem in the abstract, or with respect to ideas like ‘dynamic typing’ or ‘static typing’, is not very relevant, because in the real world we have to use real, concrete languages, and they come with a whole set of properties (in terms of the language definition, tool sets, communities and libraries) that make a massive impact on how you actually use them.

So I’m going to try to use real libraries that actually exist, ignore solutions that could theoretically exist but don’t, and ignore problems that could theoretically exist but don’t.

Python Here is my Python solution: import yaml import json json . dump ( list ( yaml . load_all ( open ( 'invoices.yaml' )))[ 0 ][ 'bill-to' ], open ( 'address.json' , 'w' )) Notes: I didn’t have to consult docs once. This isn’t just due to my familiarity with Python — it’s also the fact that I can fire up IPython and go: In [1]: import yaml In [2]: yaml.<TAB> and get a list of likely functions. I can then go: In [3]: yaml.load_all? and get help, or go: In [4]: yaml.load_all?? and get the complete source code of the function/method/class/module, in case I need it.