程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Clojure GridBag宏和print-dup大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Clojure GridBag宏和print-dup?

开发过程中遇到Clojure GridBag宏和print-dup的问题如何解决?下面主要结合日常开发的经验,给出你关于Clojure GridBag宏和print-dup的解决方法建议,希望对你解决Clojure GridBag宏和print-dup有所启发或帮助; | 我正在编写一个Clojure宏,它使用
java.awt.GrIDBagLayout
接受面板的描述,并在编译时生成等效代码(使用
(doto ...)
)。我知道跷跷板,但是正在尝试学习宏写作的来龙去脉。 我的问题: 在编译过程中的什么时候编译了Java方法(如
(Insets. 5 5 5 5)
)(生成了字节码)? 从宏返回这些值为什么会出现问题? 编译器不应该“看到”并像我手动扩展宏一样编译相同的东西吗? 由于没有相关的运行时代价,辅助函数是否可以改善情况(可能使用
(eval ...)
#=(...)
),可以返回什么? 我知道这可以写成
(defn ...)
,并且(很容易)解决问题。我想通过宏来实现相同的结果,因为我可以看到其他情况下函数的运行时性能损失可能是不可接受的(在这种情况下不可以,因为这是GUI代码)。我写这个宏的原因是因为我相信结果比手工扩展的版本更易于阅读和维护。 为了满足编译器的要求,并消除了(未成功)运行时错误消息“无法将对象嵌入代码中,也许未定义print-dup:java.awt.Insets [”,我包括了两个
(print-dup...)
多重方法的定义。顶部= 5,左侧= 5,底部= 5,右侧= 5] \“ 宏的调用方式如下:
(grID-bag-container (JPanel. (GrIDBagLayout.))
  [(Jbutton \"Monday\") :grIDwIDth 2 :weightx 1.0 :fill :HORIZONTAL]
  [(JcheckBox \"Vacation\")]
  [[(JLabel. \"Arrive:\")] [(JTextFIEld. 6) :fill :HORIZONTAL]]
  [[(JLabel. \"Depart:\")] [(JTextFIEld. 6) :fill :HORIZONTAL]])
这是预期的扩展(印刷精美,以便于阅读:-)):
(doto (JPanel. (GrIDBagLayout.))
  (.add (Jbutton \"Monday\")
        (GrIDBagConsTraints. 0 0 2 1 1.0 0
                             (. GrIDBagConsTraints West)
                             (. GrIDBagConsTraints HORIZONTAL)
                             (Insets. 2 2 2 2) 0 0))
  (.add (JcheckBox \"Vacation\")
        (GrIDBagConsTraints. 0 1 1 1 0 0
                             (. GrIDBagConsTraints West)
                             (. GrIDBagConsTraints NONE)
                             (Insets. 2 2 2 2) 0 0))
  (.add (JLabel. \"Arrive:\")
        (GrIDBagConsTraints. 0 2 1 1 0 0
                             (. GrIDBagConsTraints West)
                             (. GrIDBagConsTraints NONE)
                             (Insets. 2 2 2 2) 0 0))
  (.add (JTextFIEld. 6)
        (GrIDBagConsTraints. 1 2 1 1 0 0
                             (. GrIDBagConsTraints West)
                             (. GrIDBagConsTraints HORIZONTAL)
                             (Insets. 2 2 2 2) 0 0))
  (.add (JLabel. \"Depart:\")
        (GrIDBagConsTraints. 0 3 1 1 0 0
                             (. GrIDBagConsTraints West)
                             (. GrIDBagConsTraints NONE)
                             (Insets. 2 2 2 2) 0 0))
  (.add (JTextFIEld. 6)
        (GrIDBagConsTraints. 1 3 1 1 0 0
                             (. GrIDBagConsTraints West)
                             (. GrIDBagConsTraints HORIZONTAL)
                             (Insets. 2 2 2 2) 0 0)))
这是代码:
(defmethod print-dup java.awt.GrIDBagConsTraints [args writer]
  \"A multimethod for converTing java.awt.GrIDBagConsTraints to a compiled form.
  @param args a collection of constructor arguments
  @param writer the Writer to which the output should be generated\"
  (.write writer \"#=(java.awt.GrIDBagConsTraints. \")
  (.write writer (apply str (interpose \" \" (map str args))))
  (.write writer \")\"))

(defmethod print-dup java.awt.Insets [args writer]
  \"A multimethod for converTing java.awt.Insets to a compiled form.
  @param args a collection of (Integer) constructor arguments
  @param writer the Writer to which the output should be generated\"
  (.write writer \"#=(java.awt.Insets. \")
  (.write writer (apply str (interpose \" \" (map str args))))
  (.write writer \")\"))

(defmacro grID-bag-container [container & args]
  \"Fill a container having a GrIDBagLayout with the given components.
   The args can start with an optional default-consTraints map (see the
   doc-String for build-gbc (below) for details on the consTraints map).
   Following the optional default-consTraints are zero or more rows.
   Each row is a vector containing either a single component specification
   or multiple vectors of component specifications. Each component specification
   is a component (e.g.: Jbutton) followed by one or more key-value consTraints
   of the same form as the default-consTraints. Note that these key-value
   pairs are NOT contained in a map. Each row vector will be placed in
   the next grIDy position (starTing with 0). If a row vector contains only
   one component specification,that component will be placed at grIDx=0.
   If a row vector contains vectors,each will be placed at the next grIDx
   position (starTing with 0). The default values for the consTraints are as
   follows:
     :grIDwIDth 1
     :grIDheight 1
     :weightx 0
     :weighty 0
     :anchor :West
     :fill :NONE
     :insets (Insets. 5 5 5 5)
     :ipadx 0
     :ipady 0
   For example:
     (grID-bag-container
       (JPanel.)
       {:insets (Insets. 2 2 2 2)}              ; OverrIDe the default (Insets. 5 5 5 5)
       [button :grIDwIDth 2 :weightx 1]         ; Add a button at (grIDx=0,grIDy=0) with the
                                                ; grIDwIDth=2 (overrIDing the default 1),; and weightx=1 (overrIDing the default 0)
       [[label] [textfIEld :fill :HORIZONTAL]]) ; Add a label at (grIDx=0,grIDy=1)
                                                ; and a textfIEld at (grIDx=1,grIDy=1),; with fill=GrIDBagConTraints.CENTER
                                                ; (overrIDing the default GrIDBagConTraints.West)
   This example will expand to
     (doto container
       (.add button (build-gbc {:grIDx 0 :grIDwIDth 2 :ipadx 0 :ipady 0 :anchor :West :weighty 0
                                :grIDheight 1 :weightx 1 :fill :NONE :insets (Insets. 2 2 2 2)
                                :grIDy 0}))
       (.add label (build-gbc {:grIDx 0 :grIDwIDth 1 :ipadx 0 :ipady 0 :anchor :West :weighty 0
                               :grIDheight 1 :weightx 0 :fill :NONE :insets (Insets. 2 2 2 2)
                               :grIDy 1}))
       (.add textfIEld (build-gbc {:grIDx 1 :grIDwIDth 1 :ipadx 0 :ipady 0 :anchor :West :weighty 0
                                   :grIDheight 1 :weightx 0 :fill :HORIZONTAL :insets (Insets. 2 2 2 2)
                                   :grIDy 1})))
   @param container the java.awt.Container to fill
   @args an optional default-consTraints map followed by zero or more row specifications
   @returns the container

   build-gbc:
   Build and return a GrIDBagConsTraints containing the given consTraints map.
   Each consTraint is a (:key value) pair where the name of the key is a
   GrIDBagConsTraints fIEld (e.g.: grIDwIDth) and the value is either a keyword
   (e.g.: :CENTER),in which case the GrIDBagConsTraints constant of the same name
   (e.g.: GrIDBagConsTraints.CENTER) is used,or anything else,in which case the
   corresponding fIEld is set to that value.
   Example:
     (build-gbc {:grIDx 0
                 :grIDy 0
                 :grIDheight 1
                 :grIDwIDth 2
                 :weightx 1
                 :weighty 0
                 :anchor :CENTER
                 :fill :NONE
                 :insets (Insets. 2 2 2 2)
                 :ipadx 0
                 :ipady 0})
   will build and return a GrIDBagConsTraints containing the following fIEld values:
     grIDx 0
     grIDy 0
     grIDheight 1
     grIDwIDth 2
     weightx 1
     weighty 0
     anchor GrIDBagConsTraints.CENTER
     fill GrIDBagConsTraints.NONE
     insets (Insets. 2 2 2 2)
     ipadx 0
     ipady 0.
   @param consTraints a map containing the GrIDBagConsTraints consTraint values
   @returns a new GrIDBagConsTraints
   @see http://stuartsIErra.com/2010/01/05/taming-the-grIDbaglayout\"
  (let [global-defaults {:grIDwIDth 1
                         :grIDheight 1
                         :weightx 0
                         :weighty 0
                         :anchor :West
                         :fill :NONE
                         :insets (Insets. 5 5 5 5)
                         :ipadx 0
                         :ipady 0}
        defaults
        (if (map? (first args))
          (first args)
          {})

        args
        (into []
          (if (map? (first args))
            (rest args)
            args))

        build-gbc
        (fn [consTraints]
          (let [process-value
                #(if (nil? %)
                  nil
                  (if (keyword? %)
                    `(. GrIDBagConsTraints ~(symbol (name %)))
                    %))]
            `(GrIDBagConsTraints.
              ~(process-value (:grIDx consTraints))
              ~(process-value (:grIDy consTraints))
              ~(process-value (:grIDwIDth consTraints))
              ~(process-value (:grIDheight consTraints))
              ~(process-value (:weightx consTraints))
              ~(process-value (:weighty consTraints))
              ~(process-value (:anchor consTraints))
              ~(process-value (:fill consTraints))
              ~(process-value (:insets consTraints))
              ~(process-value (:ipadx consTraints))
              ~(process-value (:ipady consTraints)))))]
    `(doto ~container
      ~@(loop [end (count args)
               grIDy 0
               ret []]
        (if (= end grIDy)
          ret
          (let [row (nth args grIDy)
                process-item
                (fn [component grIDx grIDy consTraints]
                  (let [consTraints
                        (reducE into global-defaults
                          [{:grIDx grIDx :grIDy grIDy}
                           defaults
                           (vec (map vec (partition 2 consTraints)))])]
                    `(.add ~component ~(build-gbc consTraints))))]
            (if (vector? (first row))
              (recur end
                (inc grIDy)
                (into ret (for [grIDx (range (count row))
                                :let [item (nth row grIDX)
                                      component (first item)
                                      consTraints (rest item)]]
                  (process-item component grIDx grIDy consTraints))))
              (recur end
                (inc grIDy)
                (conj ret (let [component (first row)
                                consTraints (rest row)]
                  (process-item component 0 grIDy consTraints)))))))))))
    

解决方法

        AFAICS,您遇到的问题是您在宏中生成了Inset对象,而不是生成Inset对象的代码。 IOW,您在全局默认值中的
:insets (Insets. 5 5 5 5)
应该改为
:insets \'(Insets. 5 5 5 5)
或类似的值。 打印重复代码很混乱,不需要使用,因此就省去了。     

大佬总结

以上是大佬教程为你收集整理的Clojure GridBag宏和print-dup全部内容,希望文章能够帮你解决Clojure GridBag宏和print-dup所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。