:default multimethods unsupported by multi-spec

109 views
Skip to first unread message

Chas Emerick

unread,
Jun 20, 2016, 11:58:43 PM6/20/16
to cloju...@googlegroups.com
Example:
user> (defmulti op-type :op)
#'user/op-type
user> (s/def ::slot integer?)
:user/slot
user> (defmethod op-type :default [_]
        (s/keys :req [::slot]))
#object[clojure.lang.MultiFn 0x68573531 "clojure.lang.MultiFn@68573531"]
user> (s/explain (s/multi-spec op-type :op) {:op "foo" ::slot 5})
val: {:op "foo", :user/slot 5} fails at: [foo] predicate: user/op-type,  no method
nil
user> (op-type "foo")
#object[clojure.spec$map_spec_impl$reify__12744 0x5ac0955f "clojure.spec$map_spec_impl$reify__12744@5ac0955f"]
user> (s/explain (s/multi-spec op-type :op) {:op :default ::slot 5})
Success!
nil

multi-spec currently goes out of its way to prevent :default methods from providing specs:

https://github.com/clojure/clojure/blob/b0c945447a09137eacaa95287ce2f484f9cfdaab/src/clj/clojure/spec.clj#L903-L906

(The approach affects explicit hierarchies as well, http://dev.clojure.org/jira/browse/CLJ-1935. That makes this effectively a duplicate issue, but I thought the much more widespread usage of :default warranted a message.)

Is this likely to remain the case?

Thanks,

- Chas
Reply all
Reply to author
Forward
0 new messages