Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Clojure dynamically typed?

One thing I like very much is reading about different programming languages. Currently, I'm learning Scala but that doesn't mean I'm not interested in Groovy, Clojure, Python, and many others. All these languages have a unique look and feel and some characteristic features. In the case of Clojure I don't understand one of these design decisions. As far as I know, Clojure puts great emphasis on its functional paradigm and pretty much forces you to use immutable "variables" wherever possible. So if half of your values are immutable, why is the language dynamically typed?

The Clojure website says:

First and foremost, Clojure is dynamic. That means that a Clojure program is not just something you compile and run, but something with which you can interact.

Well, that sounds completely strange. If a program is compiled you can't change it anymore. Sure you can "interact" with it, that's what UIs are used for but the website certainly doesn't mean a neat "dynamic" GUI.

How does Clojure benefit from dynamical typing

I mean the special case of Clojure and not general advantages of dynamic typing.

How does the dynamic type system help improve functional programming

Again, I know the pleasure of not spilling "int a;" all over the source code but type inference can ease a lot of the pain. Therefore I would just like to know how dynamic typing supports the concepts of a functional language.

like image 837
lhk Avatar asked Feb 24 '11 20:02

lhk


People also ask

Is clojure a dynamic language?

First and foremost, Clojure is dynamic. That means that a Clojure program is not just something you compile and run, but something with which you can interact. Clojure is not a language abstraction, but an environment, where almost all of the language constructs are reified, and thus can be examined and changed.

Is clojure a strongly typed language?

Clojure is dynamically and strongly typed. PHP is dynamically and weakly typed. Haskell is statically and strongly typed. Java, as the most design-by-committe language ever, manages to be a mix of all four.

Is Scala statically or dynamically typed?

Scala is a unique language in that it's statically typed, but often feels flexible and dynamic.

Is OCaml dynamically typed?

OCaml gives you the best of both worlds. It's statically typed, with all the benefits that entails (including a good part of the speed of OCaml's compiled code), but you don't need to write explicit type declarations (unless you want to), because the compiler infers your types for you.


2 Answers

If a program is compiled you can't change it anymore.

This is wrong. In image-based systems, like Lisp (Clojure can be seen as a Lisp dialect) and Smalltalk, you can change the compiled environment. Development in such a language typically means working on a running system, adding and changing function definitions, macro definitions, parameters etc. (adding means compiling and loading into the image).

This has a lot of benefits. For one, all the tools can interact directly with the program and do not need to guess at the system's behaviour. You also do not have any long compilation pauses, because each compiled unit is very small (it is very rare to recompile everything). The NASA JPL once corrected a running Lisp system on a probe hundreds of thousands of kilometres away in space.

For such a system, it is very natural to have type information available at runtime (that is what dynamic typing means). Of course, nothing hinders you from also doing type inference and type checks at compilation time. These concepts are orthogonal. Modern Lisp implementations typically can do both.

like image 131
Svante Avatar answered Sep 23 '22 17:09

Svante


Well first of all Clojure is a Lisp and Lisps traditionally have always been dynamically typed.

Second as the excerpt you quoted said Clojure is a dynamic language. This means, among other things, that you can define new functions at runtime, evaluate arbitrary code at runtime and so on. All of these things are hard or impossible to do in statically typed languages (without plastering casts all over the place).

Another reason is that macros might complicate debugging type errors immensely. I imagine that generating meaningful error messages for type errors produced by macro-generated code would be quite a task for the compiler.

like image 26
sepp2k Avatar answered Sep 22 '22 17:09

sepp2k