Clojure China

Type checking for `apply f args`

#1

In Respo I defined a component like this:

(defn render [text on-click]
  (fn [state mutate]
    (div {:style {:background-color (hsl 200 80 70)
                  :display "inline-block"
                  :padding "0 8px"
                  :color "white"
                  :margin "0 8px"
                  :cursor "pointer"}
          :event {:click on-click}}
      (span {:attrs {:inner-text text}}))))

but the implementation of render function has to support arbitrary number of arguments, so I used apply render args here:

defn render-component
  markup states build-mutate coord
  let
    (begin-time $ io-get-time)
      args $ :args markup
      component $ first markup
      init-state $ :init-state markup
      new-coord $ conj coord (:name markup)
      state $ if (contains? states new-coord)
        get states new-coord
        apply init-state args
      render $ :render markup
      half-render $ apply render args
      mutate $ build-mutate new-coord
      markup-tree $ half-render state mutate
      tree $ render-element markup-tree states build-mutate new-coord new-coord
      cost $ - (io-get-time)
        , begin-time

    -- .log js/console "|markup tree:" $ pr-str markup-tree
    assoc markup :coord coord :tree tree :cost cost

then I can use this to create the component:

(comp-button "inc" handle-click)

but I found type checking is not available here, probably because I used apply. Is the any way to fix it?

#2

第二段代码语法看起来陌生,没有看懂类型检查与“apply”关系。(备注:Clojure/west 2016 上有一个关于类型演讲:Types are like the Weather, Type Systems are like Weathermen)

#3

我改了语法… 正常比如参数个数不对都能在静态检查时发现, 但用 apply 的情况检查不出来.