ClojureScript

strange extend protocol to to native case

Details

  • Type: Defect Defect
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Completed
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None

Description

Copied from gist: http://gist.github.com/cemerick/7998162

; using 2120

(defprotocol P (m [x]))

(extend-protocol P
  number
  (m [x] (- x)))

(set! (.-foo js/Number.prototype)
      #(this-as this (m this)))

cljs.user> (def j 5)
5
cljs.user> (m j)
-5
cljs.user> (.foo j)
"Error evaluating:" (.foo (inc 5)) :as "(5 + 1).foo();\n"
#<Error: No protocol method P.m defined for type object: 6>
Error: No protocol method P.m defined for type object: 6
    at :17
    at m (:20)
    at :1
    at :1
    at :5
nil
;; no idea why, but producing the same number via parsing causes the dispatch to succeed

(set! (.-foo js/Number.prototype)
      #(this-as this
         (m (js/parseFloat (str this)))))

cljs.user> (.foo j)
-5
(ns prototype-dispatch)

(defprotocol P (m [x]))
 
(extend-protocol P
number
(m [x] (- x)))
 
(set! (.-foo js/Number.prototype)
      #(this-as this (m this)))

(def j 5)

(defn ^:export -main [& args]
  (.log js/console (m j))
  (.log js/console (.foo j)))
;(set! *main-cli-fn* -main)

(try
  (-main)
  ((aget js/phantom "exit") 0)
  (catch js/Error e
    (.log js/console e)
    ((aget js/phantom "exit") 1)))

Activity

Hide
David Nolen added a comment -

not going to fix this. Inside of the prototype fn {this} will be the prototype not the Number instance.

Show
David Nolen added a comment - not going to fix this. Inside of the prototype fn {this} will be the prototype not the Number instance.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: