This is the mail archive of the
guile@sourceware.cygnus.com
mailing list for the Guile project.
Re: another nit.
hjstein@bfr.co.il writes:
> Why are you talking about using assq directly given that you have a
> get_property function wrapping it? What's the point of that?
Sorry for spreading such confusion... there are two separate issues
that we're confusing, which is
* a possible (non-)improvement to ass{q|oc|v}-ref
* what kind of functions I write in my programs.
As for the 2nd, I have a recurring pattern in LilyPond that looks like
/* pseudo codish */
SCM s = assq (key, alist));
if (s != SCM_BOOL_F && gh_SOMETYPE_p (gh_cdr (s));
{
SOMETYPE t = gh_scm2SOMETYPE (gh_cdr (s));
[..]
}
if assq-ref were to return some kind of null-value in the case of a
non-existing key (null-value = an object that fails all useful
type-predicates), I can condense the code a little:
SCM s = my_assq_ref (key, alist);
if (gh_SOMETYPE_p (s))
{
SOMETYPE t= gh_scm2SOMETYPE (s);
}
In C I can easily implement my own assq_ref that returns
SCM_UNDEFINED if a key is not there:
This seemed useful to me, and my suggestion was to extend assq-ref
extend itself in this way. However, I forgot that there are--- by
definition---no Scheme values that are `nothing', so my suggestion
would not be possible for a Scheme callable function.
Dirk suggested that I could work my way around this by defining my own
`undefined-key' smob type, and use that in stead of SCM_UNDEFINED.
On second thought however, the idea of returning a valid Scheme value
has its merits. My latest insight is that '() would be the right
return value for my application, because I also have lots of code that
does conses values onto assoc-entries, ie.
alist =
scm_assq_set (list_key, gh_cons (put_on_head, scm_assq_ref (list_key, alist)), alist)
If assq_ref returns '() for undefined keys, I don't have to initialize
entries for lists (like the entry associated with list_key in the
above example) to SCM_EOL.
(note that in my C-program, I use the convention that #t is the only
boolean that is true)
--
Han-Wen Nienhuys | hanwen@cs.uu.nl | http://www.cs.uu/~hanwen/