Questions on a very simple class

29 views
Skip to first unread message

Alain De Vos

unread,
Feb 15, 2020, 6:18:53 PM2/15/20
to Racket Users

Following code makes an "integerclass" with an "add" method :

#lang racket
(define (integerclass x)
  (define (getx)  x)
  (define (setx! x_new) (set! x x_new))
  (define (add y)(integerclass (+ x (y 'getx))))
  (lambda (message . args)
    (case message
        ((getx)      (apply getx    args))
        ((setx!)     (apply setx!   args))
        ((add)       (apply add     args))
      (else (error "POINT: Unknown message ->" message)))))

(define (myprint x) (print x)(newline))
(define p1 (integerclass 1))
(myprint (p1 'getx))
(define p2 (integerclass 2))
(myprint (p2 'getx))
(define p3 (p1 'add p2))
(myprint (p3 'getx))

Question 1 : can this code be inproved, are there "better patterns" one can you.
Question 2 : can i modify this code to use typed/racket. I tried but failed on the "apply" method in the code.

Sage Gerard

unread,
Feb 15, 2020, 6:25:41 PM2/15/20
to Alain De Vos, Racket Users
1. If the intention is to create a class, then I'd use the class form.


Not that there's anything overtly wrong with using a closure, but common validation tasks re: inheritance, overriding, etc. all have to be done by hand here.

2. What specific error message did you get?

~slg


‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.

Alain De Vos

unread,
Feb 15, 2020, 7:43:34 PM2/15/20
to Racket Users
When i try typed/racket :
#lang typed/racket

(define (integerclass x)
  (define (getx) x)
  (define (setx! [x_new : Integer]) (set! x x_new))
  (define (add [y : integerclass]) : integerclass (integerclass (+ 1 (y 'getx))))

  (lambda (message . args)
    (case message
        ((getx)      (apply getx    args))
        ((setx!)     (apply setx!   args))
        ((add)       (apply add     args))
      (else (error "POINT: Unknown message ->" message)))))

(define (myprint x) (print x)(newline))
(define p1 (integerclass 1))
(myprint (p1 'getx))
(define p2 (integerclass 2))
(myprint (p2 'getx))
(define p3 (p1 'add p2))
(myprint (p3 'getx))

I receive the errors,
On the line:
(define (add [y : integerclass]) : integerclass (integerclass (+ x (y 'getx))))
4 errors :
Type Checker: parse error in type; type name `integerclass' is unbound in: integerclass
Type Checker: parse error in type; type name `integerclass' is unbound in: integerclass
Type Checker: missing type for identifier;consider adding a type annotation with `:' identifier: integerclass in: integerclass
Type Checker: type mismatch expected: Number given: Any in: x

On the line :
((getx)      (apply getx    args))
Error :Type Checker: Bad arguments to function in `apply': Domain:  Arguments:  (Listof Any)
Other apply lines produce same errors.
To unsubscribe from this group and stop receiving emails from it, send an email to racket...@googlegroups.com.

Alain De Vos

unread,
Feb 15, 2020, 10:27:10 PM2/15/20
to Racket Users
For the record, this worked:
#lang typed/racket
(require typed/racket/class)
(define aninteger%
  (class object%
    (super-new)
    (init-field [x : Integer 0])
    (: getxinternal Integer)
       (define getxinternal x)
    (: getx (-> Integer))
       (define/public (getx) getxinternal)))
(print (send (new aninteger% [x 2]) getx))
Reply all
Reply to author
Forward
0 new messages