Excuse me, I mistook you for the OP who specifically asked:
Why is the compiler not smart enough to tell you that you tried to call the output of println (nil) as a function.
I took that as strictly a question of compile time analysis, though to be fair the OP could have just been asking "why doesn't the computer do better".
I'm aware we could instrument everything at runtime, but that's a different question with different trade-offs. Your runtime examples from other languages are interesting, I wonder if this is a particular special case though (executing a nil fn) in the plethora of possible runtime errors.
I took that as strictly a question of compile time analysis, though to be fair the OP could have just been asking "why doesn't the computer do better".
Compile-time analysis is definitely challenging in the general case, but not completely beyond reach.
Your runtime examples from other languages are interesting, I wonder if this is a particular special case though (executing a nil fn) in the plethora of possible runtime errors.
In the case of Common Lisp, these are compile-time and runtime examples. We get compile-time warnings...I don't know the concrete assumptions CL is making that allow it to infer the result of print will always be nil; they may be incompatible with assumptions (or lack thereof) in Clojure. Ex: I may freely (albeit errantly) monkey-patch println to alter the default behavior, or shadow it to a function that returns a non-nil value...in that case, all bets are off. I can suggest how to hack generic eval and apply to prevent this error, but I'm not sharp enough on compiler design (or the implementations of other lisp compilers like SBCL and CCL) to offer "real" strategies; I can only defer to my betters [who may say "there is no way"]. Perhaps applying some constraints (as I think CL might be doing) would allow increasing the strength of the static analysis the compiler can provide.
It gets back to "if we had a sufficiently smart compiler..." which in reality has yet to exist, so we would fall back on runtime instrumentation as a trade-off for dynamicity. How opposed to performance that trade-off is should be IMO up to the user (Clojure is a language for "consenting adults" as I've heard it said from the language creator and I agree).
So, perhaps instrument things "a bit" more be default to help with better error messages (stack traces are a piece of that, but not the end). Case in point: apply nil? checks for the first element of every form evaluated unless the user says otherwise (since, we know statically that nil can never applied via the language semantics). That precludes an entire class of potentially opaque errors, at a certain level of cost. It also opens the entry to say (as CL does) that the form you're evaluating (nil ...) is invalid because you're using nil as a function vs. a lame NullPointerException. Even basic exceptions like ArityException, UnsupportedOperationException provide clues far beyond NullPointerException.
I think I recall Alex Miller discussing "a bit" more instrumentation (particularly of eval) on the mailing lists some time ago, and indicating that it wouldn't happen due to the performance trade-off.
It's probably ludicrous, but I wonder how much effort it would be to maintain a fork of Clojure that instrumented a handful of core functions for basic nil checking. Pareto principle style, capture 20% of the causes. Call it safe-mode and don't expect anyone to use it in production.
Easy for me to wonder, I'm not going to do it - far too busy commenting on Reddit.
Yields an instance of clojure.lang.Compiler$InvokeExpr , which is a class that implements the clojure.lang.Compiler$Expr interface, which provides an implementation of eval.
Later, we see at line 3691 the implementation of eval for InvokeExpr (possibly one of many to be "instrumented") evals the expected function expression (fexpr) calling its .eval method, then casts it expecting an IFn. The fn.applyTo method is then invoked to finish off apply.
It "seems" like there's a really easy one-liner (for this case, there may be other expressions to hack), where you just check for (null Java) for the result of fexpr.eval ....I "think" that would enable the safety you described (at least for this case). The downside is, we now check for null potentially gajillions of times (every naïve function invocation) in exchange for helping address one class of error (unintentional null-pointers snaking into function application positions).
No idea what the performance tradeoff is (or if I'm even right). It looks like there's bytecode emission down below eval at 3706, which actually includes a checkCast for the fexpr argument apparently to check that it's an IFN (no idea if this is an actual check in bytecode, or if it's just effectively a typehint).
1
u/[deleted] Mar 22 '18
Excuse me, I mistook you for the OP who specifically asked:
I took that as strictly a question of compile time analysis, though to be fair the OP could have just been asking "why doesn't the computer do better".
I'm aware we could instrument everything at runtime, but that's a different question with different trade-offs. Your runtime examples from other languages are interesting, I wonder if this is a particular special case though (executing a nil fn) in the plethora of possible runtime errors.