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))




On Sunday, February 16, 2020 at 12:18:53 AM UTC+1, Alain De Vos wrote:
>
>
> 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.
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/3c99b587-b85a-49e5-9f7c-792a991077b4%40googlegroups.com.

Reply via email to