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

1

9

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!

Fanning answered 15/11, 2012 at 14:49 Comment(4)
typescriptlang.orgChrisom
Vote to Close; not a bad question (IMO doesn't really deserve downvotes) but likely to solicit debate so better fit for programmers.stackexchange.comBerkeleian
Any sane language, typed or not, requires declaring variables. Also, "adaptation to changing design choices" isn't necessarily easier without support from a type checker (unless you have a verbose type system).Studied
Take a look at shenlanguage.orgDove
V
7

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.

Vale answered 16/11, 2012 at 10:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.