Fengyun Liu

Papers

Blog

Quotes

Make Statically Typed Languages More Friendly

09 Feb 2022

In this post I propose that statically typed languages should have two modes:

The language design should ensure semantic equivalence of programs under the two different modes.

For the production mode, the following code problems by default should only be reported as warnings instead of errors:

Table of Contents

Problem

I am writing a simple Java program:

import java.lang.reflect.*;

public class Main {
  public static void foo(String message) {
    System.out.println(message);
  }

  public static void main(String[] args) {
    Method method = Main.class.getMethod("foo", String.class);
    method.invoke(null, "hello");
  }
}

However, the Java compiler insists that the exceptions NoSuchMethodException and IllegalAccessException must be handled or declared to throw.

import java.lang.reflect.*;

public class Main {
  public static void foo(String message) {
    System.out.println(message);
  }

  public static void main(String[] args) {
    try {
      Method method = Main.class.getMethod("foo", String.class);
      Object o = method.invoke(null, "hello");
    } catch (Exception ex) {
      System.out.println("Method not found: " + ex.getMessage());
    }
  }
}

I just want to play with the language, why it obstinately rejects to compile and run the program?

This is not or not only an issue with checked exceptions, but a common problem in statically typed languages.

Statically Typed Languages

Programming language researchers have long touted the benefits of type systems. While dynamic languages of the last century, such as JavaScript, Ruby, Python, are still active, we do witness a transition to statically typed languages. If we look at the active programming languages created in the last decade:

All of them are typed! (Julia enjoys an unobtrusive type system.)

It is widely known that statically typed languages enhance reliability and performance of programs and make code refactoring, navigation and maintenance easier. However, if type systems are so great, why we have not seen the adoption of more advanced types in daily programming, such as dependent types, linear types, session types?

Drawbacks of Type Systems

That brings us to two drawbacks of type systems:

While type inference can cut down the verbosity of type systems, real-world programming languages equipped with expressive type systems usually need manual type annotations. More expressive the type system is, more annotations are required and more difficult it is to get the types correct.

The second problem is that typing problems are reported as errors and they prevent the program from being compiled and run, which is quite annoying during prototyping, as we have seen in the case of Java checked exceptions!

Dynamic languages address the two problems by throwing types away for the sake of usability. Can we do better without throwing the baby out with the bathwater?

Prototyping and Production Mode

When a programmer is drafting a program, usually he wants to focus on the problem at hand and does not want to be distracted by typing errors. For prototyping, having the program running is more important than getting the types correct. No to mention many of such errors are usually false positives, due to the over-approximation nature of type systems and analysis.

Typing errors only become helpful during debugging and refining of the program. No one can deny that a well-typed program is better.

To address the prototyping problem, I propose that compilers should have two modes:

In the prototyping mode, some typing errors might still help the programmers in drafting the program, e.g., naming errors, and the mismatch of method arguments. These code problems can be reported as errors. It may also issue errors to protect the compiler from crash, e.g., cyclic class inheritance. However, the following code problems should only be warnings:

I can foresee it will be a technical challenge to treat some typing errors specially, due to the fact that the type system is an intricate unity. For languages that support typed-based code synthesis, e.g., Scala implicits, this is more difficult, as the meaning of the program depends on the types. Having different rules for different modes implies that a program might behave differently under a different mode, which will be a huge concern.

While maintaining semantic equivalence of programs between two modes will be an academic and engineering challenge, there are some low-hanging fruits. For example, the following code problems will not and should not incur semantic difference:

In the prototyping mode, the checks for code problems above can be skipped. In the production mode, they should by default be issued as warnings instead of errors.

Why Not An Interpreter Mode

Some readers might be wondering, why not have an interpreter mode for prototyping, so that all naming and typing errors can be delayed until run-time?

The reason is that I believe some basic error checking will still be helpful even for prototyping. While having quick runtime feedback is cool, having runtime crash can be frustrating.

From another perspective, an interpreter mode is an orthogonal design decision. What can be achieved in an interpreter mode can also be achieved in compilation mode. And the interpreter can also perform some basic type checking.

Meanwhile, the challenge of maintaining semantic equivalence of programs under different modes remains.

Conclusion

A type system can be a friendly adviser, instead of a stern rule enforcer!