Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 463
jrouquie Avatar asked Nov 15 '12 14:11

jrouquie


People also ask

Is C++ statically or dynamically typed?

Since C++ attaches values to types at compile, it follows that C++ is a statically typed language.

Is Python statically or dynamically typed?

Python is both a strongly typed and a dynamically typed language. Strong typing means that variables do have a type and that the type matters when performing operations on a variable. Dynamic typing means that the type of the variable is determined only during runtime.

Does C# use dynamic or static typing?

C# and Java are often considered examples of statically typed languages, while Python, Ruby and JavaScript are examples of dynamically typed languages.

Is Java dynamically typed or statically typed?

Java is statically-typed, so it expects its variables to be declared before they can be assigned values. Groovy is dynamically-typed and determines its variables' data types based on their values, so this line is not required.


1 Answers

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.

like image 73
ewernli Avatar answered Nov 16 '22 02:11

ewernli