[racket] inherit and apply

From: Stephen De Gabrielle (spdegabrielle at gmail.com)
Date: Wed Dec 1 15:52:47 EST 2010

Hi,

I'm getting an error when I try to use 'apply' on an inherited method:

  colourwell.rkt:60:47: class: misuse of method (not in application) in: set

I feel I've missed something in my reading of the reference manual for apply
or inheriting methods, but I don't know where I've gone wrong?

I'm sure this is a simple thing, but I'd love it if someone could tell me
where I've gone wrong.

Seasons greetings,

Stephen

---

Details follow:

I have a little class colour% to which I'm adding a simple HSV mapping

I wanted to
                  (define/public (set-hsv hue saturation value)
                    (apply set (hsv->rgb hue saturation value)))

But I had to
                   (define/public (set-hsv hue saturation value)
                    (define set-rgb (lambda (r g b) (set r g b)))
                    (apply set-rgb (hsv->rgb hue saturation value)))

I have set the class definition to  inherit 'set':
(define colour% (class color%
                  (inherit red green blue set)


-class-definition--

(define colour% (class color%
                  (inherit red green blue set)

                  (define (hsv->rgb h s v)
                    (if (= s 0)
                        (list v v v)
                        (let* ((var_h (* h 6))
                               (var_i (floor var_h));
                               (var_1 (* v (- 1 s)));
                               (var_2 (* v  (- 1 (* s (- var_h var_i)))));
                               (var_3 (* v  (- 1 (* s (- 1 (- var_h
var_i)))))))
                          (case (floor var_h)
                            ((0) {list v var_3 var_1})
                            ((1) {list var_2 v var_1})
                            ((2) {list var_1 v var_3})
                            ((3) {list var_1 var_2 v})
                            ((4) {list var_3 var_1 v})
                            (else {list v var_1 var_2})))))

                  (define (rgb->hsv r g b); -> (list h s v)
                    (define minVal (min r g b))
                    (define maxVal (max r g b))
                    (define delta (- maxVal minVal))

                    (define v maxVal)
                    (append
                     (if (= delta 0)
                         (list 0 0)
                         ;; else
                         (list (/ delta maxVal) ; s
                               (let* (
                                      (del_R (/ (+ (/ (- maxVal r) 6) (/
delta 2)) delta))
                                      (del_G (/ (+ (/ (- maxVal g) 6) (/
delta 2)) delta))
                                      (del_B (/ (+ (/ (- maxVal b) 6) (/
delta 2)) delta))
                                      (h (cond
                                           ((= r maxVal) (- del_B del_G))
                                           ((= g maxVal) (+ (/ 1 3) del_R (-
del_B)))
                                           ((= b maxVal) (+ (/ 2 3) del_G (-
del_R))))))
                                 (cond
                                   ((< h 0) (+ h 1))
                                   ((> h 1) (- h 1))))))
                     (list v)))

                  (super-new)
                  (define/public (hue)
                    (car (rgb->hsv (red) (green) (blue))))
                  (define/public (saturation)
                    (second (rgb->hsv (red) (green) (blue))))
                  (define/public (value)
                    (third (rgb->hsv (red) (green) (blue))))

                  (define/public (set-hsv hue saturation value)
                    (define set-rgb (lambda (r g b) (set r g b)))
                    (apply set-rgb (hsv->rgb hue saturation value)))
                  ))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20101201/a42f0724/attachment.html>

Posted on the users mailing list.