I guess this problem you mention must be Clojure-specific, because in Common Lisp you can declare the data types of the input parameters and all return parameters, and have the compiler (i.e. SBCL) do static type checking; also the compiler can tell you which data types are accepted and returned by your function, by use of the "describe" function.
Furthermore, you can define your own types, structs, and classes, and the declarations (and static type checks) will work with them as well.
(defmethod collide ((object1 space-ship) (object2 rocket))
"Inline documentation for this method..."
(the debris
(create-debris-from space-ship))
SPACE-SHIP and ROCKET are types and classes. DEBRIS would also be a type and a class.Use DEFTYPE, DEFCLASS, DEFSTRUCT, ... to define new types. Use CHECK-TYPE and ASSERT to have runtime checks, integrated with the condition system.
> Without type annotations and checked structured data for everything, it can also be hard to remember what a function does, or what its variables represent, when you read the code later.
I showed you how to annotate types in Common Lisp.
You also said:
> The other feature that occurs more often is in-line documentation.
Which exists in Lisp since 80s or earlier.
> but I'm really talking about compile time checks.
(declaim (ftype (function (integer string) cons)
foo))
(defun foo (a b)
(list a b))
(defun bar (a) (foo "baz" a))
Running the SBCL compiler: ; file: /private/tmp/test.lisp
; in: DEFUN BAR
; (FOO "baz" A)
;
; note: deleting unreachable code
;
; caught WARNING:
; Constant "baz" conflicts with its asserted type INTEGER.
; See also:
; The SBCL Manual, Node "Handling of Types"
At least some type checks are done at compile time...Take a look at this: http://ahungry.com/blog/2015-07-10-Type-Safety-and-Lack-Ther...
It shows you a 9-lines macro for CL, that allows you to declare types in a way resembling Haskell, and having the compiler do the static checks.
Very nice.
Another example is here: https://news.ycombinator.com/item?id=8598149
(declaim (ftype (function (integer string) cons)
foo))
(defun foo (a b)
(list a b))
(declaim (ftype (function (integer) cons)
bar))
(defun bar (a) (foo a a))
(declaim (ftype (function (integer) integer)
baz))
(defun baz (a) (+ (foo a "b") 42))
The SBCL compiler:in BAR: an argument of the wrong type.
; in: DEFUN BAR
; (FOO A A)
;
; note: deleting unreachable code
;
; caught WARNING:
; Derived type of A is
; (VALUES INTEGER &OPTIONAL),
; conflicting with its asserted type
; STRING.
; See also:
; The SBCL Manual, Node "Handling of Types"
in BAZ: the wrong return type ; compiling (DECLAIM (FTYPE # ...))
; compiling (DEFUN BAZ ...)
; file: /private/tmp/test.lisp
; in: DEFUN BAZ
; (+ (FOO A "b") 42)
;
; note: deleting unreachable code
;
; caught WARNING:
; Derived type of (FOO A "b") is
; (VALUES CONS &REST T),
; conflicting with its asserted type
; NUMBER.
; See also:
; The SBCL Manual, Node "Handling of Types"There was a study done where they had two groups of people implement the same thing, one with a dynamic language, the other with its equivalent static variant.
What they found was that the people who used the dynamic language took 33% less time to complete the task. But what's interesting is students who were given the static typed variant all said they felt the type system helped them, and made them more productive.
Now, that study can be criticised in a lot of ways, but as it stands, it's still better then people's opinions.
I think it can feel like a drag sometimes in dynamic languages to figure out the type of things, and what structure the data comming into functions has, etc. But I suspect in practice, that feeling does not translate to lower productivity or higher defects.
The obvious problem with that is that dynamically typed languages are great for prototyping things ... and implementing something quickly for a study sounds like prototyping.
But statically typed languages really shine for maintenance and refactoring.
Possibly, I've not been able to find any research related to the impact of the typing discipline on maintenance.
All there is out there are studies on defect count of large code base projects. And those showed only marginal differences, certain dynamic languages like Clojure even beat out all others.
So at least in terms of maintening quality over time, dynamic languages seem to hold up to static languages.
What I've been trying to get data on is productivity over time. I know dynamic languages are more productive at the beginning, but do static languages get more productive over time is a good question. I still feel probably not, I think it's still just all a big feeling, but I've got no data on it.
Now I'm learning Racket, and Typed Racket feels great so far (despite a few awkward spots). You don't have to sit on that fence; you can switch between typed and untyped code at will.
See my comment above, you can have static-typing in Lisp with no problem: Type declarations are part of the Common Lisp standard[1], and implementations like SBCL (one of the best Lisp implementations out there, and free) will do static type checking.
[1] type declarations: http://clhs.lisp.se/Body/d_type.htm
"the" (type specifier for return values): http://clhs.lisp.se/Body/s_the.htm#the
EDIT: a good example here:
That is an extremely controversial statement. The trouble with comments, and with programmers, is that a certain sort of brilliant programmer is very common: their intellect is fast and powerful, but they cannot contain themselves. And while their code must execute correctly, comments provide them with a limitless heaven into which they can pour their brilliance for the rest of us to read in awe, without fear of being constrained by such tedious requirements as correctness in execution, and with no requirement for precision or economy of language.
If you look at clojure.spec as a replacement for a type system, you'll be frustrated. The focus is on expressiveness, not proof. It's quite a different beast.
The leverage comes from the fact specs are data, and therefore usable at runtime, which usually isn't possible due to type-erasure.
Not having a chasm between compile-time and runtime allows extending the language. You can run clojure.spec checks, or a full type system (like Typed Clojure), etc. The entire mindset is based on "there's only runtime".
The discomfort comes from comparing that to a language that has a canonical type system, and evaluates type definitions in what is essentially a separate runtime only available in the compiler. Apples, oranges.
It is now several years later and I have earned my living ever since by working in another lisp, Clojure. And I have indeed learned a lot along the way and what I've learned does indeed translate to how I work in other languages (even C++, which I also love).
However -- it is not all roses and nicely-flavored toothpaste after a savory feast. There is one aspect to lisp programming that, by virtue of its dynamic typing, I still find myself struggling to reconcile. No matter how much I marvel at what I can do in one or two lines of lisp, I do still often find myself making dumb mistakes that a compiler would catch instantly. Without type annotations and checked structured data for everything, it can also be hard to remember what a function does, or what its variables represent, when you read the code later.
One aspect of this that has made a big difference is naming -- how you name things in lisp and dynamic languages is a skill in its own right that helps guide the readability of code in the absence of types. When I look at C++ code I've written, the verbosity of naming is a clear influence from lisp. And it is an improvement.
The other feature that occurs more often is in-line documentation. It is a joy to read Clojure programs (the good ones, anyway) where every (non-trivial or meaningful) function has a little paragraph summarising what it does. This is just good in any language, but somewhat essential in a dynamic one.
Finally, run-time contracts and things like Clojure.spec come about to help fill the void of static typing. The problem I have there is that you add back into your code a fair amount of the verbosity that was removed in the first place by using a dynamic language (just look at Clojure.spec's lengthy annotations for a function signature, and I find myself wondering... why god oh why?). At which point, I then wonder why not just go back to using a static language.
So using lisp taught me perhaps one final, frustrating lesson: there are just great things about both dynamically typed and statically typed languages, and I've learned that the fence is an awkward place to sit.