Skip to content

Instantly share code, notes, and snippets.

@johnnyb
Created April 23, 2018 12:27
Show Gist options
  • Select an option

  • Save johnnyb/211e105882248e892fa485327039cc90 to your computer and use it in GitHub Desktop.

Select an option

Save johnnyb/211e105882248e892fa485327039cc90 to your computer and use it in GitHub Desktop.
Attempt at object-oriented system
(define-syntax oo-class
(syntax-rules (attr method this)
(
(oo-class class-name
((attr attr-name initial-val) ...)
((method (meth-name meth-arg ...) body ...) ...))
(define class-name
(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
)
)
)
)
)
)
(oo-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 'next)))
(newline)
(display ((c 'nextnext)) )
(newline)
(display ((c 'next)))
(newline)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment