Type safety in Clojure
Asked Answered
L

3

18

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?

Leidaleiden answered 12/6, 2011 at 18:47 Comment(0)
K
21

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.

Kong answered 13/6, 2011 at 1:39 Comment(2)
i presume in these type systems you need to run the app in 'validation mode' to emulate compilation phase, so that it checks that and sends warnings when a call is being done with the wrong parameter type?Leidaleiden
Yes I believe both Qi/Shen and Typed Racket validate and do not compile the code if the validation pass fails.Kong
S
5

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.

Summarize answered 12/6, 2011 at 19:46 Comment(2)
So do you feel that type systems like those you mention, give the programmer the ability to offset validation checking from run-time to compile-time, or even better, design-time?Leidaleiden
Sure you could have it at compile time or with a IDE even at design time but that not the kind of development most people want for clojure so there is not much progress in that direction.Summarize
F
2

There is a library that adds an optional type system to Clojure,

http://typedclojure.org/

Rationale

Static typing has well known benefits. For example, statically typed languages catch many common programming errors at the earliest time possible: compile time. Types also serve as an excellent form of (machine checkable) documentation that almost always augment existing hand-written documentation.

Languages without static type checking (dynamically typed) bring other benefits. Without the strict rigidity of mandatory static typing, they can provide more flexible and forgiving idioms that can help in rapid prototyping. Often the benefits of static type checking are desired as the program grows.

This work adds static type checking (and some of its benefits) to Clojure, a dynamically typed language, while still preserving idioms that characterise the language. It allows static and dynamically typed code to be mixed so the programmer can use whichever is more appropriate.

Fetich answered 23/11, 2016 at 3:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.