[racket] Dynamically calling struct accessors/method names

From: Todd Bittner (toddbittner at gmail.com)
Date: Tue Sep 6 14:17:25 EDT 2011


I'm trying to something along the following lines:

> (define-struct posn (x y))
> (define my-posn (3 4))
> (define (foo posn-instance field)
        (posn-field posn-instance))

where calling

(foo my-posn "x")

would dynamically substitute posn-field with posn-x and the function would
return 3.

I don't know what to do with either "x" or posn-field to get this to work.
I *think* I'm looking for something like a string->function function, where
I could pass in "x" and posn-field would be replaced by

(string->function (string-append "posn-" field))

I just don't know if there's something that currently serves that purpose
or, if not, how to go about creating that function.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20110906/f19a6485/attachment.html>

Posted on the users mailing list.