簡體   English   中英

如何在ClojureScript中使用命名參數?

[英]How can I use named parameters in ClojureScript?

在clojure中,我可以使用defnk來獲取命名參數。 如何在ClojureScript中實現相同的功能?

ClojureScript中命名的args功能與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

如果您需要默認值,請將原始內容更改為:

(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

這與Clojure的好答案有關- 命名參數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM