liveBook · Manning

Munge

As explained in Clojure: The Essential Reference

munge is a Clojure function that translates valid Clojure names into names valid for Java classes and packages.

munge translates valid Clojure function and namespace names into names valid for Java classes and packages. Clojure permits characters such as question marks and dashes that require translation at the Java boundary; for example, namespace dashes become underscores in relative file names. section 22.3

Generated names and readable stack traces

A function named my-namespace/my-funct! can appear internally as my_namespace$my_funct_BANG_. demunge reverses that representation for display, producing my-namespace/my-funct!, while stack-element-str formats the complete stack-trace element. section 22.3

(ns my-namespace) ; #1

(require '[clojure.repl :refer [demunge stack-element-str]]) ; #2

(defn my-funct! [] (throw (ex-info "error" {})))

(str my-funct!) ; #3
;; "my_namespace$my_funct_BANG_@621ada4f"

(demunge (str my-funct!)) ; #4
;; "my-namespace/my-funct!@4e212104"

(def stack-trace (try (my-funct!) (catch Exception e (.getStackTrace e)))) ; #5

(nth stack-trace 2) ; #6
;; [my_namespace$my_funct_BANG_ invokeStatic "form-init4179141376169992155.clj" 1]

(stack-element-str (nth stack-trace 2)) ; #7
;; "my-namespace/my-funct! (form-init4179141376169992155.clj:1)"

Inspect the complete transformation from a Clojure function name to its generated Java-oriented name and back to a readable stack-trace form. section 22.3

How Clojure: The Essential Reference covers it

How deep it goes
Explains the naming boundary and dash-to-underscore consequence, but does not show munge invocation or enumerate every translation.
Examples
Uses dashed namespaces and generated function names, then contrasts their demunged stack-trace display.
What you can do after
Evaluate/understand how Clojure names appear in Java-oriented artifacts and readable stack traces.
How it connects
Connects munge with demunge, Java naming conventions, namespaces, packages, and stack traces.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.