[racket] Help With a Macro (tricky?)

From: Hal Henke (halhenke at gmail.com)
Date: Tue Jan 10 22:44:28 EST 2012

Hi Racketeers,

Just getting started in the whole Racket thing - it seems like the nicest
Lisp around at this point in time. A big reason for me wanting to get to
know Lisp/Scheme/Racket was to play around with macros and it seems like
Racket takes the idea to its logical conclusion going beyond hygenic macros
with its whole language building features. However I have to say I have
found it a little difficult to fully get my head around some of the ideas
(phase levels etc) so I suppose i could at least understand why some people
yearn for the simplicity of "defmacro" despite its flaws. I think i just
need to plow through the references some more.

Anyway for mostly self-educational purposes I was trying to get a
particular macro working and cant seem to do it. The inspiration behind it
was that I was looking at the Racket Object System and I kind of felt like
there was a little less typing to get a class going. I'm also a bit of a
fan of light feeling OO systems like in Python that dont really care about
enforcing privacy and just wanted to give the class declaration a name and
then have a public variable/property available without the need to write 3
lines. In other words instead of typing all this:

(class<http://docs.racket-lang.org/reference/createclass.html#(form._((lib._racket/private/class-internal..rkt)._class))>
 object%<http://docs.racket-lang.org/reference/createclass.html#(def._((lib._racket/private/class-internal..rkt)._object~25))>
  (init<http://docs.racket-lang.org/reference/createclass.html#(form._((lib._racket/private/class-internal..rkt)._init))>
 size)                ; initialization argument
(define<http://docs.racket-lang.org/reference/define.html#(form._((lib._racket/private/base..rkt)._define))>
 current-size size) ; field
(define/public<http://docs.racket-lang.org/reference/createclass.html#(form._((lib._racket/private/class-internal..rkt)._define/public))>
 (get-size)     current-size)  .............

I wanted to be able to write something like:

  (init+ size)

that would expand to something like:

  (init<http://docs.racket-lang.org/reference/createclass.html#(form._((lib._racket/private/class-internal..rkt)._init))>
 size)         ; initialization argument
(define<http://docs.racket-lang.org/reference/define.html#(form._((lib._racket/private/base..rkt)._define))>
 size+ size) ; field
(define/public<http://docs.racket-lang.org/reference/createclass.html#(form._((lib._racket/private/class-internal..rkt)._define/public))>
 (size++)     size+)

even better would be an expansion of (init+ size) to:
 (init<http://docs.racket-lang.org/reference/createclass.html#(form._((lib._racket/private/class-internal..rkt)._init))>
 size++)         ; initialization argument
(define<http://docs.racket-lang.org/reference/define.html#(form._((lib._racket/private/base..rkt)._define))>
 size+ size++) ; field
(define/public<http://docs.racket-lang.org/reference/createclass.html#(form._((lib._racket/private/class-internal..rkt)._define/public))>
 (size)     size+)

so that I had a public accesssor method that had the same name as the
original declaration.


As mentioned this is mainly a self-educational exercise. I know there is
"init-field" that would pretty much take care of this and there are some
prototype based Object systems in Planet but I just wanted to see if this
was possible and how/why not.

Anyway hope its not too stupid a question.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120111/554b8f88/attachment.html>

Posted on the users mailing list.