<div class="gmail_extra">#|An update: here is what the macro looks like now.|#<br>(define-syntax (define-property stx)<br> (define make-id<br> (lambda (template . ids)<br> (define str (apply format template (map syntax->datum ids)))<br>
(datum->syntax stx (string->symbol str))))<br> (syntax-parse stx <br> [((~literal define-property) name:id init-value)<br> (with-syntax ([getter-name (make-id "get-~a" #'name)]<br>
[setter-name (make-id "set-~a" #'name)])<br> #'(begin (define name init-value)<br> (define/public (setter-name value)<br> (set! name value))<br>
(define/public (getter-name)<br> name)))]<br> [((~literal define-property) name:id init-value ((~literal get:expr) get-body))<br> (with-syntax ([getter-name (make-id "get-~a" #'name)])<br>
#'(begin (define name init-value) <br> (define/public (getter-name)<br> get-body)))]<br> [((~literal define-property) name:id init-value ((~literal set) (setter-vars:id ...) set-body:expr))<br>
(with-syntax ([setter-name (make-id "set-~a" #'name)])<br> #'(begin (define name init-value)<br> (define/public (setter-name setter-vars ...)<br>
set-body)))]<br> [((~literal define-property) name:id init-value ((~literal get) get-body) ((~literal set) (setter-bindings:id ...) set-body:expr))<br> <br> (with-syntax ([getter-name (make-id "get-~a" #'name)]<br>
[setter-name (make-id "set-~a" #'name)])<br> #'(begin (define name init-value)<br> (define/public (setter-name setter-bindings ...)<br>
set-body)<br> (define/public (getter-name)<br> get-body)))]))<br><br>Somewhat like <a href="http://msdn.microsoft.com/en-US/library/w86s7x04%28v=vs.80%29.aspx">http://msdn.microsoft.com/en-US/library/w86s7x04%28v=vs.80%29.aspx</a>, but with mandatory initialization expressions for each property. <br>
<br>-Patrick<br><br><div class="gmail_quote">On 25 April 2012 10:10, Asumu Takikawa <span dir="ltr"><<a href="mailto:asumu@ccs.neu.edu" target="_blank">asumu@ccs.neu.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>On 2012-04-25 03:19:46 -0400, Patrick Mahoney wrote:<br>
> I'm looking to define a macro that extends the forms within a racket/class<br>
> class%. In particular, I would like a form to define a C# like class<br>
> language form property, that implements the getters and setters for an<br>
> initialized field automatically.<br>
<br>
</div>FYI, `get-field` and `set!-field` already allow you to get and set any<br>
public fields on an object.<br>
<div><br>
> (define-syntax (define-property syntax)<br>
> (syntax-parse syntax<br>
> [((~literal define-property) name:id init-value)<br>
> <br>
> #'(define name init-value)<br>
> (define/public (set-name! init-value)<br>
> (set! name init-value))<br>
> (define/public (get-name)<br>
> name)]))<br>
<br>
</div>So this macro won't quite work as written. Here's an alternative that<br>
will at least run (but still won't work as you expect):<br>
<br>
(define-syntax (define-property stx)<br>
(syntax-parse stx<br>
[(_ name:id init-value)<br>
#'(begin (define name init-value)<br>
(define/public (set-name! new-val)<br>
(set! name new-val))<br>
(define/public (get-name) name))]))<br>
<br>
One issue was that you were using `syntax` as the argument to this<br>
transformer, which won't work because you end up shadowing<br>
`syntax`, which is needed to write the template. I changed it to `stx`.<br>
<br>
Secondly, you need to use `begin` to splice all of the definitions<br>
together.<br>
<br>
Note that this still doesn't work though. The `set-name!` and `get-name`<br>
will be introduced as is (or hygienically renamed) and won't use the<br>
name of the field as you might want. To do that, you might want to write<br>
`define-property` so that it takes get/set method names as arguments.[1]<br>
<br>
[1]: otherwise you need to unhygienically introduce a binding, which<br>
is best to avoid unless you know what you are doing.<br>
<div><br>
> define/public: use of a class keyword is not in a class top-level in:<br>
> (define/public (set-name! init-value) (set! name init-value))<br>
<br>
</div>You probably got this error by trying out the macro outside of a class<br>
or because you weren't splicing the definition quite right.<br>
<br>
Cheers,<br>
Asumu<br>
</blockquote></div><br></div>