12

When defining a struct type and instance, I can print the value and get the "struct" implementation type:

(defstruct person :name :age)
(def p (struct person "peter" 30))

user=> p
{:name "peter", :age 30}
user=> (type p)
clojure.lang.PersistentStructMap

But is it possible to tell whether p is an instance of the struct type "person"?

2 Answers 2

8

See: this post in the Clojure Google Group. In general the group archives are a treasure chest...

Note: The functionality of structs is replaced by records. Then this is not a problem anymore, because records really define new type and you can check with instance? whether something is of record of a certain type.

Sign up to request clarification or add additional context in comments.

3 Comments

OK, thanks. Records seems to be the way to go - just need to upgrade Clojure.
Well. 1.2 is not released, yet. So YMMV.
Clojure 1.2 is now released... And hadn't come across YMMV before - a phrase I may use in future!!
4

A bit ugly, but it works:

(require '[clojure.contrib.java-utils :as cj])

(defn struct-instance? [struct-def sm]
  (= (cj/wall-hack-field clojure.lang.PersistentStructMap "def" sm)
     struct-def))

(struct-instance? person p)
; => true

(struct-instance? person (conj p [:foo 1] [:bar 2]))
; => true

(defstruct foo :k1 :k2)
(struct-instance? foo p)
; => false

Stuff from clojure.contrib.java-utils will be available in 1.2 in part in clojure.java, in part in clojure.contrib.java (I guess?), and also in clojure.contrib.java-utils for backward compatibility... Although the details might still change. At any rate, Meikel is right about defrecord replacing defstruct, so if you're working on 1.2, prefer that.

1 Comment

You certainly seem to know your way around Clojure ;) I'm at 1.1 for the moment, but the 1.2 defrecord looks like a better long-term solution. Still, thanks for the hack and the pointer to java-utils :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.