5 ms·
REPL code is copy/pasted straight into tests. So really, REPL is for helping write tests. BTW, when Clojurians talk about REPL, it's not about that separate wi
by huahaiy 2y ago
REPL code is copy/pasted straight into tests. So really, REPL is for helping write tests.
BTW, when Clojurians talk about REPL, it's not about that separate window where you type and run the code as in other language such as python. They are talking about an invisible REPL running behind the scene, to which they send code within their editors, and the results show up in the editors too.
There's no need to "miss static typing" in Clojure. If I need static typing, I just write deprotocol and deftype in Clojure, as many Clojure libraries do.
- gavmor 2y ago> copy/pasted When I've seen it done, it's seemingly executed in-place, which is very cool.
- ndr 2y agoThank you, this is the most concise description I've seen for the REPL. That gets often lost due to the curse of knowledge. It's a completely different thing to be coding "from within your running program" and it's hard to signal that to who has never tried.
- lkitching 2y agoNeither defprotocol nor deftype introduce static typing into Clojure. Errors in their usage are not checked statically and are only discovered at runtime.
- rrgok 2y agoSo what happens at runtime if errors are found?
- synthc 2y agoyou get runtime errors with a long JVM stacktrace
- erichocean 2y agoIf you paste into Claude, it will instantly tell you what's going on.
- KingMob 2y agoIf the core team had ever addressed the decade of surveys showing that error messages/stacktraces were people's top complaints, you wouldn't need Claude.
- huahaiy 2y agoNo, defprotocol and deftype have the same properties as Java interface and Java classes, and the types are checked at compile time. This is static typing. Period. Clojure is a compiled language, it does check typing during compilation.
- lkitching 2y agoProtocols do not work like Java interfaces or classes. Their methods are compiled into regular functions which lookup the implementation to use at runtime based on the runtime type of the receiver. Compilation will check for the named function but doesn't do any further checking. Given the following protocol and implementation: (defprotocol P (method [this ^Integer i])) (extend-protocol P String (method [s i] (.substring s i))) both (method "test" "call") and (method 1 2) will be accepted by the compilation phase but will fail at runtime. Of course there's no requirement for Clojure code to be AOT compiled anyway so in that case any name errors will still only be caught at runtime when the compilation happens. Type hinted bindings are only converted into a cast and are not checked at compilation time either e.g. (defn hinted [^String s] (.length s)) (hinted 3) will be accepted but fail at runtime. deftype is only used for Java interop an is also not a form of type checking. The methods will be compiled into Java classes and interfaces, but the implementations defer to regular Clojure functions which are not type checked. You can only make use of the type information by referencing the compiled class files in Java or another statically typed language, using them from Clojure will not perform type checking.
- huahaiy 2y agodeftype IS a Java class, it's not compiled into something else. What is a Clojure function? A Clojure function is a Java class. Clojure is a compiled language, so it does check types, just like Java check types. So if you use defprotocol and deftype for every domain objects in your code, your code won't compile if there's a type error. Try it. BTW, that's the way many Clojure libraries are implemented. These libraries rely on dispatch on type to work, so they are taking advantage of the type checking. Of course, you will say, "oh, clojure is not normally AOT, so it's not dong the checks.", but that's another issue. The issue at hand is this: can you write Clojure such that types are checked at compile time. The answer is YES. The compiler may run only when you run the program, that's a different issue. You are confusing these two issues. If you want a separate compile stage, then basically you are already excluding runtime compilation, i.e. you are arguing against runtime compilation. So it's not really about typing, but about how you want to run the program. Isn't it? You want AOT for everything, you don't want runtime compilation. That's it. It has nothing to do with types.