30

I have been using clojure for a couple of months now and one thing I really don't understand is why dashes in namespace names must be represented as underscores in the filesystem. Can anyone explain this to me, and is it possible to be able to use dashes in the filenames too?

yazz.com
  • 57,320
  • 66
  • 234
  • 385

2 Answers2

37

It's a necessary workaround for Java interoperability.

When a Clojure namespace is AOT (ahead-of-time) compiled into a Java .class file, it has to have a name that is a valid Java identifier. Dashes aren't valid in Java class names, so Clojure converts them to underscores. It also converts characters like * into words like _STAR_.

Stuart Sierra
  • 10,837
  • 2
  • 29
  • 35
3

Do you mean that the .class files on disk have underscores where the functions in Clojure had dashes? I'm sure I read that it's something to do with the JVM not supporting dashes in those file names. (Or at least it not being a guarantee that it supports them.)

This is only a limitation of the class file names, and Clojure silently deals with this problem anyway. Your own code can still handle files with dashes in the filename.

I'm sorry that I don't have a reference for this right now.

pauldoo
  • 18,087
  • 20
  • 94
  • 116
  • I tried putting dashes in the clj files but my clojure programs cannot read the files unless I change the dashes to underscores – yazz.com Dec 12 '10 at 13:27
  • Yes, this is because of JVM limitations. See http://clojure.org/libs "Hyphens in the lib name are replaced by underscores in the path" – Brian Carper Dec 13 '10 at 04:26
  • What does the answer mean when it says "Your own code can still handle files with dashes in the filename". Is this referring to .clj files or .class files? – yazz.com Dec 13 '10 at 22:06
  • 1
    What I meant is that when writing clojure code to handle data files on the filesystem, you can open/read/write/close files with any name you like. (Perhaps so obvious I shouldn't have mentioned it.) – pauldoo Dec 14 '10 at 16:28