2

I have the following record type that I am trying to test:

(defrecord FirstOrderState [datum matrix]
  State
  ;; implementation goes here ...
  )

I am trying to branch based on the above type, but am not getting the results I need

(def state (->FirstOrderState datum matrix))

(= (type state) composer.algorithm.markov.state.FirstOrderState)
=> false

However, looking at the type of state confirms that it should match:

(type state)
=> composer.algorithm.markov.state.FirstOrderState

This seems like it should work, as a similar check results in true:

(= (type []) clojure.lang.PersistentVector)
=> true

What is it that I am missing here? Using the below hack provides a solution, but not very elegant:

(= (str (type state)) (str composer.algorithm.markov.state.FirstOrderState))
=> true
dtg
  • 1,803
  • 4
  • 30
  • 44
  • So far I don't get it. (Not that I know very much.) It works when I try it with my own example. What happens if you use `class` instead of `type`? – Mars Dec 04 '13 at 03:34
  • `class` doesn't work either -- from what I can tell, `class` is used for Java classes, and `type` is used for Clojure types. Fore example, if I try `(instance? state composer.algorithm.markov.state.FirstOrderState)`, I get an error stating that I can't coerce `state` to a Java class. – dtg Dec 04 '13 at 04:04
  • `class` and `type` usually produce the same result, but can be made to differ. To see this, apply `class` in the `purchase-order` example on [this page](http://clojuredocs.org/clojure_core/clojure.core/type). – Mars Dec 04 '13 at 04:57

1 Answers1

5

My first guess would be that you have reloaded the namespace containing the definition of the record type and that state is defined elsewhere (possibly at the REPL), and so composer.algorithm.markov.state.FirstOrderState now refers to a different class from the one it used to at the time when state was created.

Demo at the REPL:

user=> (defrecord Foo [])
user.Foo
user=> (def foo (->Foo))
#'user/foo
user=> (= (type foo) Foo)
true
user=> (defrecord Foo [])
user.Foo
user=> (= (type foo) Foo)
false
Michał Marczyk
  • 83,634
  • 13
  • 201
  • 212