small medium large xlarge

Back to: All Forums  PragPub
Generic-user-small
07 Jul 2011, 04:16
Ambrose Bonnaire-Sergeant (3 posts)

Hi everyone,

I’ve screwed up pretty badly with my article in July’s PragPub, the errata is ridiculously long.

Here goes:

Original case expression doesn’t actually work. Should be:

(defn emit-bash-form [a] (cond (= (class a) java.lang.String) a (= (class a) java.lang.Integer) (str a) (= (class a) java.lang.Double) (str a) ))

In “emit-bash” definition, the PersistentList dispatch should be:

;; Fixed incorrect variable name (defmethod emit-bash clojure.lang.PersistentList [form] (case (name (first form)) “println” (str “echo “ (second form)) nil))

In “emit-batch” definition, the PersistentList dispatch should be:

;; Fixed incorrect variable name (defmethod emit-batch clojure.lang.PersistentList [form] (case (name (first form)) “println” (str “ECHO “ (second form)) nil))

In “emit” definition, the PersistentList dispatch should be:

;; Fixed incorrect variable name (defmethod emit [::bash clojure.lang.PersistentList] [form] (case (name (first form)) “println” (str “echo “ (second form)) nil)) (defmethod emit [::batch clojure.lang.PersistentList] [form] (case (name (first form)) “println” (str “ECHO “ (second form)) nil))

Definition of current-implementation should be:

;; def doesn’t support both metadata and docstring (def ^{:dynamic true :doc “The current script language implementation to generate”} current-implementation)

The two PersistentList dispatches in emit should be:

;; Fixed variable names (defmethod emit [::bash clojure.lang.PersistentList] [form] (case (name (first form)) “println” (str “echo “ (second form)) nil)) (defmethod emit [::batch clojure.lang.PersistentList] [form] (case (name (first form)) “println” (str “ECHO “ (second form)) nil))

Definition of script should be:

;; Forgot to unquote “impl” (defmacro with-implementation [impl & body] `(binding [current-implementation ~impl] ~@body))

Full final version of emit:

(def ^{:dynamic true :doc “The current script language implementation to generate”} current-implementation)

(defmulti emit (fn [form] [current-implementation (class form)]))

(defmethod emit [::common java.lang.String] [form] form) (defmethod emit [::common java.lang.Integer] [form] (str form)) (defmethod emit [::common java.lang.Double] [form] (str form)) (defmethod emit [::bash clojure.lang.PersistentList] [form] (case (name (first form)) “println” (str “echo “ (second form)) nil)) (defmethod emit [::batch clojure.lang.PersistentList] [form] (case (name (first form)) “println” (str “ECHO “ (second form)) nil))

(defmacro script [form] (emit '~form)) (defmacro with-implementation [impl & body] (binding [current-implementation ~impl] ~@body))

Thanks, Ambrose

Generic-user-small
07 Jul 2011, 08:15
Ambrose Bonnaire-Sergeant (3 posts)

Pg 29 Paragraph 8:

;;; Delete, makes no sense in context of article. Notice how this compares to Java inheritance, where modifications can only occur in a single namespace, often not one that you control. This common situation highlights some advantages of separating class definitions from implementation inheritance.

You must be logged in to comment