Search code examples
clojurecore.match

Clojure core.match can't match on class


When evaling this super simple core.match expression I get:

(match [(class "3.14")]
       [Integer] "Integer"
       [Double] "Doubler")
; => "Integer"

How can this be correct, am I missing something fundamental about core.match? Doing a macroexpand-1 on this form gives me:

=> (clojure.core/let [ocr-2751 (class "3.14")] (clojure.core/let [Integer ocr-2751] "Integer"))

Any pointers appreciated.


Solution

  • Like @Arthur said, normally core.match will bind values to symbols. However, apparently, it first tries to match against locals. Who knew?

    Anyway, bind the classes as locals in a let before matching and you're good to go:

    (let [Integer java.lang.Integer
          String  java.lang.String]
      (match [(class "3.14")]
             [Integer] "Integer"
             [String] "String"))