0

试图自学一些clojure,正在使用play-clj。我不明白为什么会这样:

(defn add-shape
  [x y entities]
  (write-state [x y])
  (conj entities (o-shape x y)))

虽然这没有:

(defn add-shape
  [x y entities]
  (conj entities (o-shape x y)
  (write-state [x y]))

Exception in thread "LWJGL Application" java.lang.IllegalArgumentException:No implementation of method: :draw-entity! of protocol: #'play-clj.entities/Entity found for class: java.lang.Long

这是两个相关的功能:

(defn x-shape
  [x y] 
  (shape :line 
         :line (- x 100) (- 600 100 y) (+ x 100) (- (+ 600 100) y)
         :line (- x 100) (- (+ 600 100) y) (+ x 100) (- 600 100 y)))

(defn write-state
  [arg]
  (dosync (alter state conj arg)))
4

1 回答 1

2

很确定你在第二个中有一个放错位置的括号。试试这个:

(defn add-shape
  [x y entities]
  (conj entities (o-shape x y)) ;; This is effectively a no-op, results are not used.
  (write-state [x y]))  ;; returns the results of write-state, 
                        ;; ie the result of conj-ing [x y] onto the value in ref state 

但是,我现在认为您的问题的根源是两个版本具有不同的返回值。这是第一个返回的内容:

(defn add-shape
  [x y entities]
  (write-state [x y])
  (conj entities (o-shape x y))) ;; returns the results of conj-ing the results of (o-shape x y)
                                 ;; onto the passed-in value of entities

TLDR:函数返回不同的值,您的程序可能仅适用于您的第一个结果。

于 2014-08-11T16:59:54.077 回答