[plt-scheme] changing the name of a struct field accessor

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sun Nov 29 10:54:20 EST 2009

At Sat, 28 Nov 2009 12:51:19 -0500, Neil Van Dyke wrote:
> Is there a way to change the name of a struct field accessor produced by 
> "make-struct-field-accessor"?
> The name that "make-struct-field-accessor" generates seems somewhat 
> specific to its use by "define-struct".
>  From a quick look at the 4.2.2 C code, it appears that this name cannot 
> be changed, but perhaps I'm missing something.
> As a workaround, I could wrap the struct field accessor procedure in a 
> procedure that has the desired inferred name.  But then I'd have to take 
> a small performance hit, or hope the compiler optimized without losing 
> my inferred name.

A wrapper procedure would be a fine choice within a module, but across
modules you'd need cross-module inlining (to appear sometime next year,
maybe) to avoid a performance hit (which is a factor of 4, since the
JIT detects struct accessors and handles them specially).

Meanwhile, there's an obvious place in the implementation to drop a
performance hack. I've changed `procedure-rename' so that it recognizes
a struct accessor or mutator, and in that case it creates a new
primitive one:

 (define-struct s (f))
 (define alt (procedure-rename s-f 'alt))

There's an advantage to this trick over using a macro to force
cross-module inlining: if the wrong kind of argument is supplied to the
accessor, then the error message uses the new name instead of the
original one. That's a feature we'd often like to have when renaming a
function, but I've never figured out how to make that work in general.

Posted on the users mailing list.