Skip to content

Instantly share code, notes, and snippets.

@rpgoldman
Created August 18, 2011 02:03
Show Gist options
  • Save rpgoldman/1153123 to your computer and use it in GitHub Desktop.
Save rpgoldman/1153123 to your computer and use it in GitHub Desktop.
Problem with protocol implementation...
(ns rpg.bug
(:require clojure.contrib.math))
(defprotocol CPARTS
(real [c])
(imag [c])
(modulus [c])
(phase [c]))
(defrecord complex [real imag]
CPARTS
(real [c] (:real c))
(imag [c] (:imag c))
(modulus [c]
(clojure.contrib.math/sqrt
(+ (* (real c) (real c)) (* (imag c) (imag c)))))
(phase [c]
(Math/atan (/ (imag c) (real c)))))
(defn mod2 [c]
(clojure.contrib.math/sqrt
(+ (* (real c) (real c)) (* (imag c) (imag c)))))
;;; works:
;;; (mod2 (complex. 1 1))
;;; crashes:
;;; (modulus (complex. 1 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment