Misleading strong type systems

This post is a record of a Dart Misc mailing list post titled "Types are useless (?)".

The original author was curious why Darts "allows running code with invalid arguments".  My reply follows:


For reference, let's look at Java. Many would think that Java has a "strong
type system" and that its types prevent, as you say, invalid arguments.
However, it's easy to write a Java program that will compile just fine, yet
fail at runtime. In your words, it will allow running code with invalid
arguments. Here's an example:

import java.util.*;

public class hacked {
  public static void test(int i) {

  public static void main(String[] args) {
    String msg = "hacked";

    Map cache = new HashMap();
    cache.put("hacked", msg);

    // later
    int hacked = (Integer) cache.get("hacked");


This code will compile, yet fail at runtime. (Yes, I could have used
generics to help a bit, but they aren't mandatory)

I will assert that even with something like Java's type system, which
creates barriers and hoops for developers, one can still write incorrect
code. In fact, any time you have to cast, you are trying to express
something your type system can't express.

So, what if a language has a new perspective on static types? What if a
language used static types as annotations, which helped you without getting
in your way? Your tools still give you warnings and errors, yet your
programs will run without burdensome ceremony.

There is a direct tradeoff between constraining type systems that disallow
any ever possible error (which is also probably impossible), and developer
productivity. Remember that Dart is trying to appeal to a wide array of
developers, from JavaScript developers that deal only in functions and have
never seen a type, to Java/C# developers that love their tools, static
types, and structure. Dart is optimizing for developer productivity
without sacrificing structure or tooling. Its optional types help you when
you need them, but don't create unnecessary barriers.

I personally felt more free once I realized that no mainstream language
actually had a perfect type system. A combination of tool support, static
analysis, checked mode assertions, and unit tests can allow me to feel just
as safe, yet more productive.

Thank you for your questions, it's healthy to be skeptical. :)


Popular posts from this blog

Converting Array to List in Scala

Lists and arrays in Dart

Null-aware operators in Dart