Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic functionality of cljc files

Normally, Clojure source files are named (for example) foo.clj, and Clojurescript source files are named foo.cljs. My impression is that in Clojure versions >= 1.7, I can name a file foo.cljc if I want it to be available for loading with require or use both from Clojure and Clojurescript.

Is this correct? It seems to be implicit in the primary documentation pages on Using cljc and reader conditions, but as far as I can see it's never explicitly stated.

This is not a question about using reader conditionals to specify alternate code for running in Clojure and Clojurescript; it's more fundamental. For example, I have a source file that contains code that's completely generic: It will run in both Clojure and Clojurescript unchanged. Can I assume that by naming it with ".cljc", require will always find it from inside both Clojure and Clojurescript (assuming it's named correctly, is in the correct location, etc.)?

[I'm pretty sure that I'm right, but I'm not certain, and I thought it would be worth having the answer documented here if I'm correct.]

like image 951
Mars Avatar asked Jul 30 '16 18:07

Mars


1 Answers

That's correct. When Clojure or ClojureScript needs to load a namespace, they first look for the platform-specific file (.class then .clj on Clojure and .cljs on ClojureScript) and if not found, then they look for the .cljc file.

You do still need to make sure it is available to the language on the classpath, as with other source files.

like image 143
Alex Miller Avatar answered Sep 23 '22 05:09

Alex Miller