Re: Mixing syntax-rule and indentifier-syntax
On Wed 18 Jan 2012 00:26, Tobias Brandt writes: >> 0. I'm sure there is another way, but my mind blanks at the moment > > After you got me started, I was able to simplify and generalize it a bit. > (-> is a GOOPS generic accessor I defined elsewhere. It works with > vectors, arrays, strings, etc ...) > (define-syntax with-accessors (lambda (stx) (syntax-case stx () > ((_ (id ...) exp ...) #`(begin #,@(let (;; checks if My eyes!!! ;-) Can you repost, but without having your editor wrap the lines? Thanks :) Andy -- http://wingolog.org/
Re: Accessing multiple values from C
On Wed 18 Jan 2012 04:15, Mark H Weaver writes: > Should this verify that `obj' is a values object? > > Should it verify that `idx' can fit in an inum? Agreed! > Also, if `obj' is _not_ a values object and `idx' is 0, should this > simply return `obj'? Since a single value is conceptually no different > than multiple values (but is represented very differently within Guile), > I'd think that this function should handle that case gracefully. Hum, good question. I'm inclined to agree. Regards, Andy -- http://wingolog.org/
Re: Accessing multiple values from C
Hi Mark, Mark H Weaver skribis: > Julian Graham writes: >> +SCM >> +scm_c_value_ref (SCM obj, size_t idx) >> +{ >> + SCM values = scm_struct_ref (obj, SCM_INUM0); >> + return scm_list_ref (values, SCM_I_MAKINUM (idx)); >> +} >> + > > Should this verify that `obj' is a values object? > > Should it verify that `idx' can fit in an inum? Yes, good point. Using an ‘SCM_VALIDATE’, which will throw an exception upon error, right? > Also, if `obj' is _not_ a values object and `idx' is 0, should this > simply return `obj'? Yes, makes sense to me. Julian: could you change these bits and send an updated patch? Thanks, Ludo’.
Re: Accessing multiple values from C
Hi all, Thanks for the review! On Wed, Jan 18, 2012 at 3:19 PM, Ludovic Courtès wrote: > Julian: could you change these bits and send an updated patch? Will do. Regards, Julian
Re: Accessing multiple values from C
l...@gnu.org (Ludovic Courtès) writes: > Mark H Weaver skribis: > >> Julian Graham writes: >>> +SCM >>> +scm_c_value_ref (SCM obj, size_t idx) >>> +{ >>> + SCM values = scm_struct_ref (obj, SCM_INUM0); >>> + return scm_list_ref (values, SCM_I_MAKINUM (idx)); >>> +} >>> + >> >> Should this verify that `obj' is a values object? >> >> Should it verify that `idx' can fit in an inum? > > Yes, good point. Using an ‘SCM_VALIDATE’, which will throw an exception > upon error, right? Well, there's a complication with using an `SCM_VALIDATE' to check for values objects. If `idx' is 0 (make sure it's exact!), then we must _not_ throw an exception if `obj' is not a values object. However, in that case we still need to know whether `obj' is a values object or not. Also, a fine point about desirable error messages: asking for `idx' 5 of a values object with only 3 elements is, conceptually, the same kind of error as asking for `idx' 5 of something that is _not_ a values object. Therefore, I think in both cases the error should be something to the effect of "too few values". Thanks, Mark
Re: Accessing multiple values from C
Hi! Mark H Weaver skribis: > l...@gnu.org (Ludovic Courtès) writes: > >> Mark H Weaver skribis: >> >>> Julian Graham writes: +SCM +scm_c_value_ref (SCM obj, size_t idx) +{ + SCM values = scm_struct_ref (obj, SCM_INUM0); + return scm_list_ref (values, SCM_I_MAKINUM (idx)); +} + >>> >>> Should this verify that `obj' is a values object? >>> >>> Should it verify that `idx' can fit in an inum? >> >> Yes, good point. Using an ‘SCM_VALIDATE’, which will throw an exception >> upon error, right? > > Well, there's a complication with using an `SCM_VALIDATE' to check for > values objects. If `idx' is 0 (make sure it's exact!), then we must > _not_ throw an exception if `obj' is not a values object. However, in > that case we still need to know whether `obj' is a values object or not. Yes, I already agreed on this one. ;-) > Also, a fine point about desirable error messages: asking for `idx' 5 of > a values object with only 3 elements is, conceptually, the same kind of > error as asking for `idx' 5 of something that is _not_ a values object. > > Therefore, I think in both cases the error should be something to the > effect of "too few values". Right. Ludo’.
Re: Accessing multiple values from C
I think something along these lines be ideal, though this code is UNTESTED. Mark SCM scm_c_value_ref (SCM obj, size_t idx) { if (SCM_VALUESP (obj)) { SCM values = scm_struct_ref (obj, SCM_INUM0); size_t i = idx; while (SCM_LIKELY (!scm_is_null (values))) { if (i == 0) return SCM_CAR (values); values = SCM_CDR (values); i--; } } else if (idx == 0) return obj; scm_error (scm_out_of_range_key, "scm_c_value_ref", "Too few values in ~S to access index ~S", scm_list_2 (obj, scm_from_unsigned_integer (idx)), scm_list_1 (scm_from_unsigned_integer (idx))); }
Re: Accessing multiple values from C
At Julian's request, I went ahead and pushed my implementation of `scm_c_value_ref' to the stable-2.0 branch, so it will be in 2.0.4. Thanks, Mark