deftype

Difference between deftype in Common Lisp and Scheme

I'm trying to translate some Common Lisp code into Scheme code. The Common Lisp code has a deftype. Are deftypes in Scheme the same as deftypes in Common Lisp? How do you translate a deftype in Common Lisp into equivalent code in Scheme? ...

Nested types in clojure?

In clojure, how do I type type hint a type that I have created? (I want to nest the types.) e.g. I had thought that this would work: (deftype A [#^somePrimitive someField]) (deftype B [#^A Avalue]) This brings up an error message: Unknown location: error: java.lang.ClassNotFoundException: A Note: clojure types are a...

When should I use deftype in Clojure?

Yesterday, Rich pulled the 'new' branch of Clojure into master. We are now embracing the beauty that is deftype and defprotocol. Of course, I, coming from Haskell, am very tempted to define types like I would in Haskell, which would be for virtually everything short of a throwaway tuple, but I don't think it works like that in Clojure wo...

Can I add fields to clojure types?

Clojure structs can be arbitrarily extended, adding new fields. Is it possible to extend types (created using deftype) in a similar way? EDIT: For the benefit future visitors, as Brian pointed out below, this feature is subject to change. ...

How can I define a clojure type that implements the servlet interface?

I'm attempting to use deftype (from the bleeding-edge clojure 1.2 branch) to create a java class that implements the java Servlet interface. I would expect the code below to compile (even though it's not very useful). (ns foo [:import [javax.servlet Servlet ServletRequest ServletResponse]]) (deftype servlet [] javax.servlet.Servle...

Overriding equals, hashCode and toString in a Clojure deftype

I'm trying to create a new type in Clojure using deftype to implement a two dimensional (x,y) coordinate, which implements a "Location" protocol. I'd also like to have this implement the standard Java equals, hashCode and toString methods. My initial attempt is: (defprotocol Location (get-x [p]) (get-y [p]) (add [p q...

Mutable fields in Clojure deftype?

I'm trying out Clojure 1.2, specifically mutable fields which are supported in deftype according to the clojure.org documentation. But I can't get the set to work. What is the syntax for updating a field? Or isn't mutability implemented yet? (definterface IPoint (getX []) (setX [v])) (deftype Point [x] IPoint (getX [this] x) ...

Using Clojure deftype as a parameterized function

I am trying to use clojure in a compiler and thus need to parameterize calls to deftype; however, I am having difficulty making the type hints carry through. Consider the following code: (defn describe [x] (let [fields (.getDeclaredFields x) names (map #(.getName %) fields) types (map #(.getType %) fields)] (inte...

What's a good toString method for a deftype'd object in clojure

(deftype Bag [state] Object (toString [bag] (str "Bag???" state))) I want the toString to look something like clojure.core=> (def b (Bag. {:apples 1 :bannanas 4})) #'clojure.core/b clojure.core=> (str b) "BAG: {:apples 1 :bannanas 4}" What is a nice clojurey way of representing that information? Is "Bag/{:k :v}" ...

How do you use a type outside of its own namespace in clojure?

I have a project set up with leiningen called techne. I created a module called scrub with a type in it called Scrub and a function called foo. techne/scrub.clj: (ns techne.scrub) (deftype Scrub [state] Object (toString [this] (str "SCRUB: " state))) (defn foo [item] (Scrub. "foo") "bar") techne/scrub_test.clj...