Skip to content

Instantly share code, notes, and snippets.

@johnnyb
Last active April 23, 2018 20:53
Show Gist options
  • Save johnnyb/0e295c4bc64b7e37878fd6b3a4e21cdd to your computer and use it in GitHub Desktop.
Save johnnyb/0e295c4bc64b7e37878fd6b3a4e21cdd to your computer and use it in GitHub Desktop.
This works with "this"
(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