Question

There are a lot of questions on SO about static vs dynamic typing, but I haven't found a lot about a language having both. Let me explain.

It seems that dynamically typed languages have an edge when it comes to rapid prototyping, e.g. Python or Perl, while statically typed languages (like C++, OCaml) allow for more compile-time checks and optimizations. I'm wondering if there is a language that would allow both:

  • first, rapidly prototype with dynamic typing, generic (i.e. accepting any type) print functions for easy debugging and REPL, and adaptation to changing design choices
  • then, change a few things and compile the code into a library, with static typing for more safety tests and best performance. The things one changes to allow static typing could be for instance: declaring variables (but not annotating everything, thanks to type inference), adding a compiler switch, using specific functions instead of generic ones, etc.

In C# the default is static typing, but you can write:

dynamic fooVar = new FooClass();

in which case fooVar is dynamically typed.

It seems that OCaml with http://www.lexifi.com/blog/runtime-types also offer something like this.

Please no subjective advice about which language is best, only objective features!

Was it helpful?

Solution

Sure. It's called "gradual typing", and I would qualify it as trendy.

A cousin of "gradual typing" is "optional typing". In both cases, code with and without static types coexist. However, in "optional typing", the semantics of the language is completely agnostic of static types, while in "gradual typing" the semantics might consider static types, if they are available.

From the page of the course "Integrating Static and Dynamic Typing", I read they study

The design of recent languages that integrate static and dynamic typing, including Typed Racket (formerly Typed Scheme), C# 4.0, Diamondback Ruby, Haskell, Sage, and Thorn

You can add Dart to the list, which proposes optional typing as in the position paper Pluggable, Optional Type Systems.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top