Issue with spec-ing higher-order functions; clojure.lang.AFunction$1 cannot be cast to clojure.lang.MultiFn

66 views
Skip to first unread message

Josip Gracin

unread,
Jan 17, 2019, 7:38:44 AM1/17/19
to Clojure
Hi!

The following test throws an exception when run using "lein test" and I can't figure out why.

(ns issue
  (:require  [clojure.test :as t]
             [clojure.spec.alpha :as s]
             [clojure.spec.test.alpha :as st]))

(s/fdef ::boolean-supplier
  :args (s/cat)
  :ret boolean?)

(defn make-fn [boolean-supplier]
  (fn []
    (boolean-supplier)))

(s/fdef make-fn
  :args (s/cat :supplier ::boolean-supplier))

(t/deftest issue
  (t/testing "issue demo"
    (st/instrument)
    (make-fn (fn [] true))))

Here's the output of "lein test":

$ lein test                       
                                                         
lein test issue                                                
                                                                  
lein test :only issue/issue                                                      
                                                           
ERROR in (issue) (Compiler.java:7647)                          
Uncaught exception, not in assertion.                                                  
expected: nil                                                                 
  actual: clojure.lang.Compiler$CompilerException: Syntax error compiling at (clojure/test/check/clojure_test.cljc:95:1).
#:clojure.error{:phase :compile-syntax-check, :line 95, :column 1, :source "clojure/test/check/clojure_test.cljc"}
 at clojure.lang.Compiler.load (Compiler.java:7647)                          
    clojure.lang.RT.loadResourceScript (RT.java:381)                 
    clojure.lang.RT.loadResourceScript (RT.java:372)                                            
    clojure.lang.RT.load (RT.java:463)                          
    clojure.lang.RT.load (RT.java:428)
    ...
Caused by: java.lang.ClassCastException: clojure.lang.AFunction$1 cannot be cast to clojure.lang.MultiFn
 at clojure.core$get_method.invokeStatic (core.clj:1817)
    clojure.core$get_method.invoke (core.clj:1817)                                     
    clojure.test.check.clojure_test$eval1131.invokeStatic (clojure_test.cljc:95)
    clojure.test.check.clojure_test$eval1131.invoke (clojure_test.cljc:95)
    clojure.lang.Compiler.eval (Compiler.java:7176)                          
    clojure.lang.Compiler.load (Compiler.java:7635)
    ...

Using Java 10.0.2 and the following dependencies:

$ lein deps :tree
 [clojure-complete "0.2.5" :exclusions [[org.clojure/clojure]]]
 [nrepl "0.5.3" :exclusions [[org.clojure/clojure]]]
   [nrepl/bencode "1.0.0"]
 [org.clojure/clojure "1.10.0"]
 [org.clojure/core.specs.alpha "0.2.44"]
 [org.clojure/spec.alpha "0.2.176"]
 [org.clojure/test.check "0.9.0"]

Any ideas?

Alex Miller

unread,
Jan 17, 2019, 9:26:24 AM1/17/19
to Clojure
Your boolean-supplier should be:

(s/def ::boolean-supplier
  (s/fspec
    :args (s/cat)
    :ret boolean?))

Josip Gracin

unread,
Jan 17, 2019, 10:09:01 AM1/17/19
to Clojure
Thanks!  I understand why my version is incorrect.  However, I get the same error with the corrected spec.

Alex Miller

unread,
Jan 17, 2019, 10:48:09 AM1/17/19
to clo...@googlegroups.com
The modified version works for me, so not sure what you're seeing.

$ clj
Clojure 1.10.0
user=> 
(ns issue
  (:require  [clojure.test :as t]
             [clojure.spec.alpha :as s]
             [clojure.spec.test.alpha :as st]))
nil

issue=> 
(s/def ::boolean-supplier
  (s/fspec
    :args (s/cat)
    :ret boolean?))
:issue/boolean-supplier

issue=>
(defn make-fn [boolean-supplier]
  (fn []
    (boolean-supplier)))
#'issue/make-fn

issue=>
(s/fdef make-fn
  :args (s/cat :supplier ::boolean-supplier))
issue/make-fn

issue=>
(t/deftest issue
  (t/testing "issue demo"
    (st/instrument)
    (make-fn (fn [] true))))
#'issue/issue

issue=> (issue)
nil



--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/EkAgPjrree0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Josip Gracin

unread,
Jan 18, 2019, 2:49:22 AM1/18/19
to clo...@googlegroups.com
Thanks! I've created an issue in Leiningen tracker
(https://github.com/technomancy/leiningen/issues/2524) because it
might have to do with it.
Reply all
Reply to author
Forward
0 new messages