Last active
April 23, 2018 20:53
-
-
Save johnnyb/0e295c4bc64b7e37878fd6b3a4e21cdd to your computer and use it in GitHub Desktop.
This works with "this"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define-syntax class | |
(syntax-rules (attr method this) | |
( | |
(class class-name | |
((attr attr-name initial-val) ...) | |
((method (meth-name meth-arg ...) body ...) ...)) | |
(define class-name | |
(let | |
( | |
(result (quote (lambda () | |
(letrec | |
( | |
(this #f) | |
(attr-name initial-val) | |
... | |
(funcmap | |
(list | |
(cons (quote meth-name) (cons (lambda (meth-arg ...) body ...) '())) | |
... | |
) | |
) | |
) | |
(set! this (lambda (methname) | |
(cadr (assoc methname funcmap)) | |
)) | |
this | |
) | |
))) | |
) | |
(eval result) | |
) | |
) | |
) | |
) | |
) | |
(class Counter | |
( | |
(attr value 0) | |
(attr skip 1) | |
) | |
( | |
(method (next) (set! value (+ value skip)) value) | |
(method (nextnext) ((this 'next)) ((this 'next))) | |
(method (set-value newval) (set! value newval)) | |
(method (set-skip newskip) (set! skip newskip)) | |
) | |
) | |
(define c (Counter)) (display ((c 'next))) (newline) (display ((c 'nextnext)) ) (newline) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment