简体   繁体   中英

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-rectangle and carpet are structs.

When this method is called like so:

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

I'm getting a:

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

Is it meant to be like this? Perhaps there is some way to make it accept nil as an argument? How would I specify that only arguments of type nil and carpet are applicable?

If you have an arglist with classes carpet and image-rectangle , the arguments better be of these classes or their subclasses. You can't pass NIL , when your argument is declared to be of class carpet .

Thus (if this does not make sense. If you pass a carpet object, and you can't pass anything else, the test this will always be true.

If you want to write a method for the NIL object and a rectangle, then you can use the class NULL .

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

Since CLOS does not have class combinators like OR or AND, you have to write a method for each case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM