Skip to content

Instantly share code, notes, and snippets.

@shaunlebron
Last active June 18, 2016 16:37
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaunlebron/a98a05b47a1521b58a6b to your computer and use it in GitHub Desktop.
Save shaunlebron/a98a05b47a1521b58a6b to your computer and use it in GitHub Desktop.
protocol implementation in cljs
;; dummy protocol
(defprotocol IFoo
(foo [this]))
;; dummy cljs type
(deftype Bar [])
;; extending a cljs type
(extend-type Bar
IFoo (foo [this] (println "Hello from IFoo.foo for Bar")))
;; extending a js type
(extend-type js/Function
IFoo (foo [this] (println "Hello from IFoo.foo for js/Function")))
;; extending a js base type
(extend-type number
IFoo (foo [this] (println "Hello from IFoo.foo for number")))
//// cljs.user=> (extend-type Bar IFoo (foo [this] (println "Hello from IFoo.foo for Bar")))
(function() {
cljs.user.Bar.prototype.cljs$user$IFoo$ = true;
return cljs.user.Bar.prototype.cljs$user$IFoo$foo$arity$1 = (function(this$) {
var this$__$1 = this;
return cljs.core.println.call(null, "Hello from IFoo.foo for Bar");
});
})()
//// cljs.user=> (extend-type js/Function IFoo (foo [this] (println "Hello from IFoo.foo for js/Function")))
(function() {
Function.prototype.cljs$user$IFoo$ = true;
return Function.prototype.cljs$user$IFoo$foo$arity$1 = (function(this$) {
var this$__$1 = this;
return cljs.core.println.call(null, "Hello from IFoo.foo of js/Function");
});
})()
//// cljs.user=> (extend-type number IFoo (foo [this] (println "Hello from IFoo.foo for number")))
(function() {
(cljs.user.IFoo["number"] = true);
return (cljs.user.foo["number"] = (function(this$) {
return cljs.core.println.call(null, "Hello from Foo.foo of number");
}));
})()
@viebel
Copy link

viebel commented Mar 23, 2016

Hey Shaun.
Now, with KLIPSE you can generate dynamic gists for clojurescript code.
Have a look here KLIPSE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment