vydd/sketch

incf in a let form

Closed this issue · 2 comments

In common lisp, we are allowed to incf in a let form as per these examples (link: https://jtra.cz/stuff/lisp/sclr/incf.html):

(let ((a 10)) (incf a) a) => 11
(let ((a 10)) (incf a 2.3) a) => 12.3
(let ((a 10)) (incf a -2.3) a) => 7.7
(let ((a (list 10 11 12 13))) (incf (elt a 2) 2.3) a) => (10 11 14.3 13)

However, when I have a let form in Sketch and I incf a lexically scoped variable, I get the following error:

Error - The value NIL is not of type (UNSIGNED-BYTE 32) when binding SB-ALIEN::VALUE

The code:

(dotimes (x 10)
  (dotimes (y 10)
    (with-pen (make-pen :fill (let ((x 0.01))
                                (rgb x x x) 
                                (incf x 0.01))
                        :stroke (gray 0.2)
                        :weight 11)
      (rect (* x 80) (* y 80) 80 80))))

Does anyone know why the (incf) in the let triggers the unsigned-byte 32 error?

The problem is not with incf but with the return value of let. You create a pen with :fill being equal to a number - 0.02, which causes an error when trying to draw something.

Aha! You are correct, and I am dumb. For anyone reading this, the updated code is the same, I just changed the order of forms as the let returns the last form in the body:

(dotimes (x 10)
  (dotimes (y 10)
    (with-pen (make-pen :fill (let ((x 0.01))
                               (incf x 0.01))
                                (rgb x x x) 
                        :stroke (gray 0.2)
                        :weight 11)
      (rect (* x 80) (* y 80) 80 80))))

thanks Gleefre for the tip!