Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type safety in Clojure

I want to ask what sort of type safety languages constructs are there on Clojure?

I've read 'Practical Clojure' from Luke VanderHart and Stuart Sierra several times now, but i still have the distinct impression that Clojure (like other lisps) don't take compilation-time validation checking very seriously. Type safety is just but one (very popular) strategy for doing compilation-time checking of correct semantics

I'm asking this question because i'm aching to be proven wrong; what sort of design patterns are there available on clojure to validate (at compilation-time, not at run-time) that a function that expects a string doesn't get called with, say, a list of integers?

Also, i've read very smart people like Paul Graham openly advocate about lisp allowing to implement everything from lower-level languages on top of it (most would say that the language themselves are being reimplemented on top of it), so if that assertion would be true, then trivially stuff like type checking should be a piece of cake. So do you feel that there exist type systems (or the ability to implement such type systems) in clojure or other lisps, that give the programmer the ability to offset validation checking from run-time to compile-time, or even better, design-time?

like image 630
lurscher Avatar asked Jun 12 '11 18:06

lurscher


People also ask

Is Clojure strongly typed?

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.

Why doesn't clojure have types?

It says that "Clojure is dynamic" not that Clojure is dynamically typed (although it is). That is different emphasis. It says that most things in Clojure are reified and can be changed at runtime (e.g. namespaces). This is pragmatic choice and no one objects if you add-on some type restrictions (see e.g. type hints).

What is type safety in Golang?

TypeSafe means that variables are statically checked for appropriate assignment at compile time. For example, consder a string or an integer. These two different data types cannot be cross-assigned (ie, you can't assign an integer to a string nor can you assign a string to an integer).

Is Lisp statically typed?

Static type checking in the programmable programming language (Lisp) There is a misconception that Lisp is a dynamically typed language and doesn't offer compile-time type checking. Lisp being the programmable programming language that is of course not true.


2 Answers

Since Clojure is a dynamic language the whole idea is not to check the types (or much of anything) at compile time.

Even when you add type hints to your function they do not get checked at compile-time.

Since Clojure is a Lisp you can do whatever you want at compile-time with macros and macros are powerful enough that you can write your own type systems. Some people have made type systems for lisps Typed Racket and Qi. These Type systems can be just as powerful as any Type system in a "normal" language.

Ok, we now know that it is possible but does Clojure has such a optional type system? The answer is currently no but there is a logic engine (core.logic) that could be used to implement a typesystem but the author has not worked (yet) in that direction.

like image 34
nickik Avatar answered Oct 11 '22 10:10

nickik


Compilation units in Clojure are very small - a single function. Lispers tend to change small portions of running programs while they develop. Introducing static type checking into this style of development is problematic - for a deeper discussion why I recommend the post Types are Anti-Modular by Gilad Bracha. Thus Clojure's prefers pre/post-conditions which jive better with Lisp's highly REPL-oriented development.

That said, it's certainly desirable and possible to build an a la carte type system for Clojure. This trail has been blazed by Qi/Shen, and Typed Racket. This functionality could be easily provided as a library. I'm hoping to build something like that in the future with core.logic - https://github.com/clojure/core.logic.

like image 170
dnolen Avatar answered Oct 11 '22 10:10

dnolen