deftype

Interface which contains conj?

☆樱花仙子☆ 提交于 2020-01-14 10:33:34
问题 As an exercise, I am developing a data structure similar to Vector. I have implemented all interfaces which IPersistentVector extends, but I have not found the interface where 'conj' is defined. Which interface is that? Thanks! 回答1: clojure.lang.IPersistentCollection/cons . It was named cons originally, and that's stuck around in the interface even though the Clojure function for it is now called conj . 来源: https://stackoverflow.com/questions/8781213/interface-which-contains-conj

Mutable fields in Clojure deftype?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-20 09:16:11
问题 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) (setX [this v] (set! (.x this) v))) user=> (def p (Point. 10)) user=> (.getX p) 10 user=> (.setX p 20) ClassCastException: user.Point cannot be cast to compile__stub.user

Clojure - deftype ignored - unable to resolve classname on tests

最后都变了- 提交于 2019-12-12 14:13:55
问题 I'm testing deftype and defprotocol in Clojure, but I'm in a bit of a pickle. I'm using leiningen. My core file ( src/core.clj ) looks like this: (defprotocol Speaker (say [speaker message])) (deftype Person [name] Speaker (say [_ message] (str name ": " message))) My test file ( test/core.clj ) looks like this: (deftest people-can-talk (is (= "Peter: hello" (say (Person. "Peter") "hello")))) When I execute that test (with lein test ) I get the following error: Exception in thread "main" java

Setting a mutable field in a nested function - deftype - clojure

核能气质少年 提交于 2019-12-11 01:48:51
问题 EDIT: After posting the previous version of my question I discovered that the real problem is with nested functions. If I have a closure within a deftype I can not update any mutable fields from within that closure. E.g. the following works: (deftype Test [^:unsynchronized-mutable x] TestInterface (perform [this o] (set! x o))) but this does not: (deftype Test [^:unsynchronized-mutable x] TestInterface (perform [this o] (fn [] (set! x o)) nil)) ; throws a compiler error about assigning to non

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

隐身守侯 提交于 2019-12-06 18:50:19
问题 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: (ns techne.scrub-test (:use [techne.scrub] :reload-all) (:use [clojure.test])) (deftest test-foo (is (= "bar" (foo "foo")))) (deftest test-scrub (is (= (Scrub. :a) (Scrub. :a)))) When

Mutable fields in Clojure deftype?

半城伤御伤魂 提交于 2019-12-02 18:13:19
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) (setX [this v] (set! (.x this) v))) user=> (def p (Point. 10)) user=> (.getX p) 10 user=> (.setX p 20) ClassCastException: user.Point cannot be cast to compile__stub.user.Point Using a 1.2 snapshot from a few days ago. deftype 's default is still to have the fields be