Re: [racket-users] Questions on a very simple class

2020-02-15 Thread Alain De Vos
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 getxargs))
((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 getxargs))
Error :Type Checker: Bad arguments to function in `apply': Domain:  
Arguments:  (Listof Any)
Other apply lines produce same errors.

On Sunday, February 16, 2020 at 12:25:41 AM UTC+1, Sage Gerard wrote:
>
> 1. If the intention is to create a class, then I'd use the class form.
>
>
> https://docs.racket-lang.org/reference/createclass.html#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._class%2A%29%29
>
> 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 ‐‐‐
> On Saturday, February 15, 2020 6:18 PM, 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 getxargs))
> ((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...@googlegroups.com .
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/racket-users/c28892b8-e49b-4257-9ebe-4560943e87a7%40googlegroups.com
>  
> 
> .
>
>
>

-- 
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/644612e1-ee15-4acb-b2b9-ed8c94f85f86%40googlegroups.com.


Re: [racket-users] Questions on a very simple class

2020-02-15 Thread Sage Gerard
1. If the intention is to create a class, then I'd use the class form.

https://docs.racket-lang.org/reference/createclass.html#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._class%2A%29%29

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 ‐‐‐
On Saturday, February 15, 2020 6:18 PM, 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 getxargs))
> ((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/c28892b8-e49b-4257-9ebe-4560943e87a7%40googlegroups.com](https://groups.google.com/d/msgid/racket-users/c28892b8-e49b-4257-9ebe-4560943e87a7%40googlegroups.com?utm_medium=email_source=footer).

-- 
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/gUC9HzrnJWNGbe-kgcoyov8KOUFY7sKw4KNu2CfqFABrsW0dsGpLsPfuKAHmcoXMBFECT4Bln1wQMYhpOFhL_y4WjYY-RWnveapTNESXHoc%3D%40sagegerard.com.