Idiom to fetch key and datum Takeshi Abe (07 Sep 2015 08:25 UTC)
Re: Idiom to fetch key and datum John Cowan (07 Sep 2015 17:58 UTC)
Re: Idiom to fetch key and datum taylanbayirli@xxxxxx (07 Sep 2015 18:13 UTC)
Re: Idiom to fetch key and datum John Cowan (07 Sep 2015 18:23 UTC)
Re: Idiom to fetch key and datum taylanbayirli@xxxxxx (07 Sep 2015 19:40 UTC)

Re: Idiom to fetch key and datum taylanbayirli@xxxxxx 07 Sep 2015 18:13 UTC

John Cowan <xxxxxx@mercury.ccil.org> writes:

> Takeshi Abe scripsit:
>
>> Just a minor point, but can the idiom code be:
>> (let ((key (ephemeron-key ephemeron)))
>>   (if (ephemeron-broken? ephemeron-broken)
>>       ... broken case ...
>>       (let ((datum (ephemeron-datum ephemeron)))
>>         ... code using key and datum ...)))
>> in order to spare the useless access to datum when the ephemeron
>> has already been broken?
>
> Unfortunately the answer is no.  In a system where GC runs in a separate
> thread, the ephemeron may break after calling ephemeron-broken? and
> before calling ephemeron-datum.  You really do have to capture both
> key and datum first.

It might be a good idea to encode the pattern into the API, like:

    (let-values (((broken? key datum) (ephemeron-ref ephemeron)))
      (if broken?
        ... broken case ...
        ... use key and/or datum ...))

The 'ephemeron-ref' procedure would be shown as the *primary* way to
access an ephemeron's contents (i.e. not merely represented as a
"helper" or "convenience" procedure), and the more primitive forms
offered only for backwards compatibility, and perhaps convenience, but
in any case de-emphasized because of their problematic nature.

This is the same kind of issue as with 'file-exists?' which was
criticized for inviting programmers into writing error-prone code as
some might remember.

Taylan