[racket] Size matters

From: Tim Brown (mightyfoo at gmail.com)
Date: Mon Jun 10 08:20:55 EDT 2013

On 09/06/13 18:54, Matthias Felleisen wrote:
>       (define item (first items))
>       (define weight (item-weight item))
>       (define volume (item-volume item))
>       (define value (item-value item))

> Now I understand that the three lines of extracting the content of the
> first struct are painful. While I understand and agree with the match
> here, I have also experimented with a struct-open construct for my
> latest project, and I find this code as readable as the one with match:

I also find structs quite cumbersome to use, although infinitely better
than (define name (first item)) etc.

Is there a lot of work going into structs at the moment?

I find them particulary hard to use in a purely functional (non-mutable)
way, since there is no shorthand way of copying a struct. AFAICT, at the
moment, to copy an item I need to:

   (struct item (name explanation value weight volume) #:prefab)
   (define gold (item "gold (bars)" "Shiney shiney" 2500 2.0 0.002))
   (define fools-gold
     (item-name gold)
     (item-explanation gold)
     (item-weight gold)

Possibly with some variation on extracting the fields, using match or
whatever. Even with a match, you still need to evaluate each of the
fields; and if I want to copy one field from an n-field struct, I need n
field accessors, and n fields in the constructor (with one of them

What I would really like to have is the following defined by (struct item):

   (define fools-gold (copy-item gold #:value 2.50))

Am I missing a trick?


Posted on the users mailing list.