![](/img/trans.png)
[英]attempt to call method 'print' (a nil value) when implementation OOP in Lua
[英]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
和carpet
是结构。
当这样的方法被调用时:
(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
作为一个论点? 我如何指定只有nil
和carpet
类型的参数适用?
如果你有一个班的arglist carpet
和image-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.