-
Notifications
You must be signed in to change notification settings - Fork 212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mx/defn and optional named arguments? #1003
Comments
I have just literally tried the same thing; (require '[malli.destructure :as md])
(md/parse '[& {:keys [a :- :int]}])
;; => {:raw-arglist [& {:keys [a :- :int]}],
;; :parsed {:elems [], :rest {:amp &, :arg {:arg [:map {:keys [a :- :int]}]}}},
;; :arglist [& {:keys [a :- :int]}],
;; :schema
;; [:cat
;; [:altn
;; [:map
;; [:map
;; [:a {:optional true} :any]
;; [:- {:optional true} :any]
;; [:int {:optional true} :any]]]
;; [:args
;; [:*
;; [:alt
;; [:cat [:= :a] :any]
;; [:cat [:= :-] :any]
;; [:cat [:= :int] :any]
;; [:cat :any :any]]]]]]}
(md/parse '[& {:keys [a]} :- [:map [:a :int]]])
;; => {:raw-arglist [& {:keys [a]} :- [:map [:a :int]]],
;; :parsed
;; {:elems [],
;; :rest {:amp &, :arg {:arg [:map {:keys [a]}], :- :-, :schema [:map [:a :int]]}}},
;; :arglist [& {:keys [a]}],
;; :schema [:cat [:map [:a :int]]]}
The second case is roughly what we want the first case to do I believe. |
Actually the second case also doesn't work, because the instrumented function is expected to have arity 1 rather than arity 2. So unless there is another syntax to use I don't think |
Thank you @larkery for the speedy answer. I'll move away from the named arguments style and pass in an opt map. |
There is no support for defining types for destructured keys atm. The Schematize Syntax is taken from Plumatic Schema as Cursive understands that too. Support for this would be simple to add but would add ambiquity issue, e.g. the following (valid clojure!) would not work: (let [{:keys [a :- :int]} {:a 1, :- 2, :int 3}]
[a - int])
; => [1 2 3] So, this does not work: (md/parse ['{:keys [a :- :int]}])
;{:raw-arglist [{:keys [a :- :int]}],
; :parsed {:elems [{:arg [:map {:keys [a :- :int]}]}], :rest nil},
; :arglist [{:keys [a :- :int]}],
; :schema [:cat
; [:altn
; [:map [:map [:a {:optional true} :any] [:- {:optional true} :any] [:int {:optional true} :any]]]
; [:args [:schema [:* [:alt [:cat [:= :a] :any] [:cat [:= :-] :any] [:cat [:= :int] :any] [:cat :any :any]]]]]]]} But this does: (md/parse ['{:keys [a]} :- [:map [:a :int]]])
;{:raw-arglist [{:keys [a]} :- [:map [:a :int]]],
; :parsed {:elems [{:arg [:map {:keys [a]}], :- :-, :schema [:map [:a :int]]}], :rest nil},
; :arglist [{:keys [a]}],
; :schema [:cat [:map [:a :int]]]} IMO Clojure should add support for optional type definitions, via plain (md/parse ['{:keys [a : :int]}])
;{:raw-arglist [{:keys [a : :int]}],
; :parsed {:elems [{:arg [:map {:keys [a]}], EMPTYKW EMPTYKW, :schema [:map [:a :int]]}], :rest nil},
; :arglist [{:keys [a : :int]}],
; :schema [:cat [:map [:a :int]]]} |
I'm attempting to spec a function where I destructure named arguments with defaults. I'm not sure where I'm going wrong here:
I'm new to malli, so it might be something obvious I'm missing.
The text was updated successfully, but these errors were encountered: