简体   繁体   中英

How can I use named parameters in ClojureScript?

In clojure I can use defnk to get named parameters. How can I achieve the same thing in ClojureScript?

The named args functionality in ClojureScript is the same as in Clojure:

(defn f [x & {:keys [a b]}] 
  (println (str "a is " a " and b is " b)))

(f 1)
; a is  and b is 

(f 1 :a 42)
; a is 42 and b is 

(f 1 :a 42 :b 108)
; a is 42 and b is 108

If you want default values, then change the original to:

(defn f [x & {:keys [a b] :or {a 999 b 9}}]
  (println (str "a is " a " and b is " b)))

(f 1)
; a is 999 and b is 9

This is related to the nice answer for Clojure - named arguments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM