簡體   English   中英

第一個參數為nil時無法調用方法?

[英]Can't call method when first argument is nil?

(defmethod carpet-append ((this carpet) (rect image-rectangle))
  (destructuring-bind (rect-width . rect-height)
      (rectangle-size rect)
    (destructuring-bind (bitmap-width . bitmap-height)
        (carpet-size this)
      (if this
          (iter:iter
            (iter:with min-area = (* (+ bitmap-width rect-width)
                                     (+ bitmap-height rect-height)))
            (iter:with min-pos = nil)
            (iter:for pos in (awailable-positions this))
            (iter:for test-area = (try-fit rect pos (carpet-bitmap this)))
            (when (and test-area (< test-area min-area))
              (setf min-pos pos))
            (iter:finally
             (let ((new-carpet
                    (make-carpet
                     :bitmap (make-array
                              (list (+ (car min-pos) rect-width)
                                    (+ (cdr min-pos) rect-height))
                              :element-type 'bit)
                     :rectangles (cons rect (carpet-rectangles this)))))
               (copy-bitmap-state this new-carpet)
               (setf (rectangle-position rect) min-pos)
               (place-image new-carpet rect)
               (return new-carpet))))
          (make-carpet
           :bitmap (make-array
                    (list rect-width rect-height)
                    :element-type 'bit)
           :rectangles (list rect))))))

image-rectanglecarpet是結構。

當這樣的方法被調用時:

(carpet-append
 nil
 #s(image-rectangle
    :position (0 . 0)
    :size (48 . 76)
    :file "/home/wvxvw/projects/spritesheet/test-images/test-0.png"))

我得到了:

#<SIMPLE-ERROR "~@<There is no applicable method for the generic function ~2I~_~S~
 ~I~_when called with arguments ~2I~_~S.~:>"

這是不是意味着這樣? 也許有一些方法可以讓它接受nil作為一個論點? 我如何指定只有nilcarpet類型的參數適用?

如果你有一個班的arglist carpetimage-rectangle ,參數更好的是這些類或子類的。 當您的參數被聲明為類carpet時,您無法傳遞NIL

因此, (if this是沒有意義的。如果你傳遞地毯對象,你無法通過其他任何東西,測試this將永遠是正確的。

如果要為NIL對象和矩形編寫方法,則可以使用NULL類。

(defmethod carpet-append ((this null) (rect image-rectangle))
   ...)

由於CLOS沒有OR或AND等類組合器,因此必須為每種情況編寫一個方法。

暫無
暫無

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

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