Clojure - protocols/multimethods overflow -


in order better understand clojure protocols, asking myself if act cond. instance function may overflow :

(defn my-cond [n]   (cond     (< n 0) (my-cond (inc n))     (> n 0) (my-cond (dec n))     :else "zero")) 
> (my-cond 3) ;; "zero"  > (my-cond 99999999) ;; java.lang.stackoverflowerror 

for instance, let's use protocol make equivalent (ie. recursive protocol call). change in way way stack blow ?

my intuition says no (how be), (1) have no understanding of protocol internals , (2) since make code less coupled, makes easier introduce kind of loop , make sense able prevent it.

do protocols , multimethods use stack in same way normal method calls ?

yes; functions, methods, multimethods , protocols push context onto stack. protocols differ function call conditional or multimethod because protocol exposes single dispatch on type, , jvm fast @ that. types make protocols usable java in ways dynamic function not. yes semantically same thing, fulfill practical need of speed , interop underlying platform.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -