Created
June 21, 2017 02:35
-
-
Save bhb/7eefe3034a0622b6499a5b5dd2f7e52a to your computer and use it in GitHub Desktop.
Should I be able to provide a default spec with the `:default` implementation of my multimethod?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; [org.clojure/clojurescript "1.9.229"] | |
(s/def :event/type keyword?) | |
(s/def :search/url string?) | |
(s/def :error/code int?) | |
(defmulti event-type :event/type) | |
(defmethod event-type :event/search [_] | |
(s/keys :req [:event/type :search/url])) | |
(defmethod event-type :event/error [_] | |
(s/keys :req [:event/type :error/code])) | |
(defmethod event-type :default [_] (s/keys)) | |
(s/def :event/event (s/multi-spec event-type :event/type)) | |
(comment | |
(s/valid? :event/event {:event/type :event/error :error/code 1}) ; Valid | |
;; event-type does return a spec for unknown types | |
(s/form (event-type {:event/type :event/unknown})) ; => (cljs.spec/keys) | |
;; But the unknown event is not valid | |
(s/valid? :event/event {:event/type :event/unknown}) ; => false | |
(s/explain :event/event {:event/type :event/unknown}) ; val: {:event/type :event/unknown} fails spec: :event/event at: [:event/unknown] predicate: event-type, no method | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment