Is there a language that allows both static and dynamic typing?

There are many questions about SO about static and dynamic typing, but I have not found much about a language that has both. Let me explain.

Dynamically typed languages ​​seem to have an advantage when it comes to rapid prototyping, for example. Python or Perl, while statically typed languages ​​(for example, C ++, OCaml) allow for additional checks and optimization of compilation time. I am wondering if there is a language that would allow:

  • first a quick prototype with dynamic typing, a common (i.e., any type of reception) print function for easy debugging and REPL, as well as adaptation to changing design options
  • then change a few things and compile the code into a library, with static typing for safer tests and better performance. Things that can be changed so that static typing can be, for example: declaring variables (but not annotating everything thanks to type inference), adding a compiler, using certain functions instead of general ones, etc.

C # uses static printing by default, but you can write:

dynamic fooVar = new FooClass(); 

in this case fooVar dynamically typed.

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

Please, not subjective advice, which language is best, only objective functions!

+4
source share
1 answer

Sure. It was called "gradual typing," and I would call it fashionable.

The cousin of "gradual input" is "optional typing." In both cases, code with and without static types coexists. However, in “optional typing”, the semantics of the language are completely agnostic for static types, while in “gradual typing” semantics can consider static types, if available.

From the course page " Integrating Static and Dynamic Input, " I read what they learn

Design of the latest languages ​​that combine static and dynamic, including Typed Racket (formerly Typed Scheme), C # 4.0, Diamondback Ruby, Haskell, Sage and Thorn

You can add Dart to a list that offers optional text input, as in the paper position of Pluggable, Optional Type Systems .

+5
source

Source: https://habr.com/ru/post/1446259/


All Articles