2014-04-05 2 views
2

def* 매크로에 선택적 docstring을 추가하고 싶습니다. 예를 들어 :def * 매크로에 선택적 docstring 추가

(defmacro defhtml 
    "Macro to avoid backtick unquote[splicing] in html vectors. 
    TODO: Add optional docstring." 
    [name args & body] 
    `(defn ~name ~args (html [email protected]))) 

;; Working defhtml 
(defhtml include-css [href] 
    [:link {:href href :rel "stylesheet"}]) 

내가 좋아하는 것 :

(defhtml include-css 
    "My optional docstring here." 
    [:link {:href href :rel "stylesheet"}]) 

내가 이것에 대한 몇 가지 일반적인 관용구가 있어야한다 그림.

+0

당신이 선택 문서화 문자열을 추가 할 생각에 * 당신의 데프 * 매크로 *의 호출이 이렇게 구현 될 수 defhtml의 경우입니다. – Thumbnail

답변

5

매크로에 대한 두 번째 인수가 doc-string인지 여부를 결정해야합니다 (문자열인지 테스트 할 수 있음). Clojure 매크로는 Clojure이므로 원하는 매크로에 전달 된 양식에서 모든 논리 또는 조작을 수행 할 수 있습니다. 이 가까이 있어야하는 당신이있어 정확히 어떤 경우 후 :

(defmacro defhtml [name & args] 
    (cond 
    ;; doc-string? 
    (string? (first args)) 
    (let [[doc-string args-list & body] args] 
     `(defn ~name ~doc-string ~args-list (html [email protected]))) 

    :no-doc-string 
    (let [[args-list & body] args] 
     `(defn ~name ~(format "HTML Generator %s" name) ~args-list (html [email protected]))))) 

후있어 매크로 확장을 생산해야 :

(defhtml include-css [href] 
    [:link {:href href :rel "stylesheet"}]) 

가 생성됩니다

(defn include-css 
    "HTML Generator include-css" 
    [href] 
    (html [:link {:href href, :rel "stylesheet"}])) 

동안 :

(defhtml include-css 
    "Standard css includes fory my site" 
    [href] 
    [:link {:href href :rel "stylesheet"}]) 

:

(defn include-css 
    "Standard css includes fory my site" 
    [href] 
    (html [:link {:href href, :rel "stylesheet"}])) 
2

defndefmacro 이미 옵션 문서화 문자열을 지원하므로 def* 매크로 이들 중 하나에 대한 호출로 확장하는 경우, 당신은 당신의 자신의 코드에 어떤 string? 검사를 포함 할 필요가 있습니다.

;; also adding an optional attribute map parameter, because why not? 
(defmacro defhtml [name doc? attr-map? params & body] 
    `(defn ~name ~doc? ~attr-map? ~params (html [email protected]))) 

;; equivalent, perhaps prettier: 
(defmacro defhtml [name doc? attr-map? params & body] 
    `(defn [email protected][name doc? attr-map? params] (html [email protected]))) 
+0

'def'는 선택적인 docstring 인자도 받아 들인다. 그러나 이것은 선택적인 docstring과 * body (임의의 많은 표현식)를 취하고'def'로 직접 확장하는 매크로를 작성하는 데 별 도움이되지 않는다. –