【问题标题】:Can't call method when first argument is nil?第一个参数为零时无法调用方法?
【发布时间】:2012-12-19 03:10:55
【问题描述】:
(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 类型的参数适用?

【问题讨论】:

    标签: oop lisp common-lisp clos


    【解决方案1】:

    如果您有一个包含 carpetimage-rectangle 类的 arglist,则参数最好是这些类或其子类。当您的参数被声明为 carpet 类时,您不能通过 NIL

    因此(if this 没有意义。如果您通过了一个地毯对象,但您不能通过其他任何东西,那么测试 this 将始终为真。

    如果你想为NIL对象和一个矩形写一个方法,那么你可以使用NULL类。

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

    由于 CLOS 没有 OR 或 AND 之类的类组合符,因此您必须为每种情况编写一个方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-28
      • 1970-01-01
      • 2012-09-02
      • 2015-03-21
      相关资源
      最近更新 更多