Proposal: Hide the int in opApply from the user

Bill Baxter dnewsgroup at billbaxter.com
Mon Jan 7 16:24:27 PST 2008


Bruce Adams wrote:
> On Mon, 07 Jan 2008 09:06:52 -0000, Bill Baxter 
> <dnewsgroup at billbaxter.com> wrote:
> 
>> I proposed this iniitally over in D.learn, but I'm cleaning it up and
>> reposting here in hopes of getting some response from Walter who was
>> probably too busy finishing const and eating holiday Turkey at the time
>> to notice.  And rightly so.
>>
>> I don't believe it's appropriate in a high-level supposedly clean
>> language like D that one of the main facilities for iterating over user
>> types (foreach) requires writing code that passes around magic values
>> generated by the compiler (opApply).
>>
>> It seems wrong to me that these magic values
>> - come from code generated by the compiler,
>> - must be handled exactly the proper way by the user's opApply
>>     (or else you get undefined behavior, but no compiler errors)
>> - and then are handed back to code also generated by the compiler.
>>
> When you say magic value what do you mean? From the context it sounds 
> like you
> are describing an iterator without iterators being properly part of the 
> D world (yet).

I mean this:

alias int MagicValue;

MagicValue opApply(MagicValue delegate(ref T) dg)
{
    MagicValue magic_value=0;
    foreach(x; stuff) {
        magic_value = dg(x);
        if (magic_value != 0) return magic_value;
    }
    return magic_value;
}

"Magic value" is that int that we're forced to have scattered all over 
our opApply functions.

--bb



More information about the Digitalmars-d mailing list