This is the mail archive of the guile@sources.redhat.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: struct bug


On Fri, 28 Jul 2000, Clark McGrew wrote:

> I was playing with structs this morning, and I noticed that the
> documentation example snarfed into struct.doc doesn't work right.
> 
> boxer:mcgrew-~/tmp$ guile
> guile> (version)               
> "1.4"
> guile> (define x (make-vtable-vtable (make-struct-layout (quote pw)) 
> 				     0 
> 				     'foo))
> guile> (struct-ref x vtable-offset-user)
> #f
> guile> vtable-offset-user
> 4
> guile> (struct-ref x 3)                  
> foo
> 
> However:
> 
> guile> (define y (make-vtable-vtable (make-struct-layout (quote pw)) 
> 				     0 
> 				     'foo 'bar))
> guile> (struct-ref y vtable-offset-user)
> bar
> 
> I don't know how structs are suppose to work so I can't tell if this
> is a documention error, or a bug in the struct code.

That part of the struct code is a mess:  The documentation and
implementation both hold inconsistencies.  From the sources I found that
the first parameter ('foo in your example) will be used as a printing
function.  Thus, you should be able to specify how objects of that type
will print.

From the NEWS about guile-1.3:

** There is now a fourth (optional) argument to make-vtable-vtable and
   make-struct when constructing new types (vtables).  This argument
   initializes field vtable-index-printer of the vtable.

However, this is IMO complete rubbish:  How do you distinguish an optional
fourth argument from the other rest arguments ?  And, the way the code
works, there is no distinction made.  Thus, any first initializer is used
to initialize the printing function slot.

So, why doesn't this lead to errors if structs of that type are
printed?  Well, since the printing function parameter is 'optional' the
printing function first checks, whether the slot actually holds a
function.  If so, it is called, otherwise, the default printing mechanism
is used.

Thus, your paramter 'foo initializes the slot for the printing function,
but since it is no function, it is never used :-)

If, instead of 'foo you put a function there, say a thunk, then you will
get an error as soon as you want to print a struct of that type:

guile> (define x
         (make-vtable-vtable (make-struct-layout (quote pw))
                             0
                             (lambda () #f)))
guile> (make-struct x 0 42)
ERROR: Wrong number of arguments to #<procedure ()>
ABORT: (wrong-number-of-args)


The good news about all this is, that probably nobody actually uses
structs, because otherwise this error would have been detected much
earlier :-)


However, I don't know how to fix it:  It's just not possible to say that
the first elements of the init arguments is an optional printing
function.  You can't distinguish this from an init argument which is also
a function.

Best regards
Dirk Herrmann


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]