Skip to content

Instantly share code, notes, and snippets.

@frankhale
Last active December 30, 2015 19:29
Show Gist options
  • Save frankhale/7874327 to your computer and use it in GitHub Desktop.
Save frankhale/7874327 to your computer and use it in GitHub Desktop.
Can't figure out how to implement an interface in Clojure CLR
; This does not work, boo! Don't know how to tell Clojure that IsReusable is a property
(System.Reflection.Assembly/LoadWithPartialName "System.Web")
(defn foo-handler []
(reify System.Web.IHttpHandler
(IsReusable [] false)
(ProcessRequest [context] ())))
@frankhale
Copy link
Author

I haven't found much on Google for examples on implementing interfaces. I did find this one which is an example for Clojure JVM:

http://mylesmegyesi.blogspot.com/2011/12/implementing-java-interfaces-in-clojure.html

@frankhale
Copy link
Author

This seems to work, at least the compiler doesn't complain:

user=> (def foo-handler
    (reify System.Web.IHttpHandler
            (get_IsReusable [this] false)
            (ProcessRequest [this context] ())))
#'user/foo-handler
user=> (instance? System.Web.IHttpHandler foo-handler)
true
user=>

@frankhale
Copy link
Author

This is better and works fine in an ASP.NET app:

(deftype foo-handler []
    System.Web.IHttpHandler
    (get_IsReusable [this] false)
    (ProcessRequest [this context] 
        (.Write (.Response context) "Hello, From Clojure CLR!")))

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