i'm a clojure newbie and am pretty thoroughly confused by defprotocol, 
inline implementation and extend-type

i have the following two small files: crecords.tproc & crecrods.trec listed 
below:  

*A)   crecords.tproc:*

(ns crecords.tproc)

(defprotocol Fruit   (subtotal [this]))

(defrecord Orange [qty])

(extend-type Orange
  Fruit
  (subtotal [this] (* 2 (:qty this))))

(defn -main []
  ;; runs ok - note Absence of '.' before subtotal
  (println "Orange Subtotal:" (subtotal (Orange. 4)))) 


*B)   crecords.trec:*

(ns crecords.trec
  (:require [crecords.tproc :refer [Fruit]]))

(defrecord Apple [qty]
  Fruit
  (subtotal [this] (* 5 (:qty this))))

(defrecord Banana [qty])

(extend-type Banana
  Fruit 
  (subtotal [this] (* 3 (:qty this))))

(defn f1 []
  ;; No matching field found: subtotal for class crecords.trec.Banana
  (println "Banana Subtotal:" (.subtotal (Banana 2)))) 

(defn f2 []
  ;; Runs fine - Note the Presence of '.' before subtotal
  (println "Apple Subtotal:" (.subtotal (Apple. 8)))) 

(defn -main []
  ; (f1) - Can't compile/run - see f1 comments
  (f2) ;; runs fine - see f2 comments
)

When i run "lein run -m crecords.tproc", it produces the right answers.
When i run "lein run -m crecords.trec", calling (f2) in main, it produces 
the right answers.
When i run "lein run -m crecords.trec", calling (f1) in main, it crashes.

Would seriously appreciate a good answer to the following:
1.  Why *doesn't* extend-type (see Banana) work when implementing  protocol 
Fruit in a different namespace
2.  Why *does* inline  implementation (see Apple) work when implementing 
protocol Fruit in  a different namespace
3.  Why the different function call "subtotal" (*without* Dot, see Apple) 
vs ".subtotal" (*with* Dot, see Orange)  for extend-type vs inline 
implementation of Fruit?

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to