assignment: left-to-right or right-to-left evaluation?

Nick Sabalausky a at a.a
Mon May 11 23:12:44 PDT 2009


"Rainer Deyke" <rainerd at eldwood.com> wrote in message 
news:guatj2$2pft$1 at digitalmars.com...
> Nick Sabalausky wrote:
>> "Rainer Deyke" <rainerd at eldwood.com> wrote in message
>> news:guamfi$2b0d$1 at digitalmars.com...
>>> [[mydic[x], mydic[y], mydic[z]].each].each = mydic.length;
>>> ...
>>>
>>
>> I'm not sure what point you're trying to make here, but my idea is that
>> "each" would basically be a write-only "extension property" (to borrow C#
>> terminology) of array. So "each" wouldn't have a getter or a return value
>> and therefore trying to stick its non-existant read/return value into an
>> array literal (as you're doing above) would be an error.
>
> I had assumed that there was some sort of magic going on that would
> allow this.  If '[mydic[x], mydic[y], mydic[z]].each' is a write-only
> property of an array, then assigning to it will modify the array, but
> leave 'mydic' untouched.
>

I wrote up a short app to try to think my idea through a bit better. This is 
the best I was able to get in D1:

-------------------------
import tango.io.Stdout;

void each(T, V)(T*[] array, V val)
{
    foreach(T* elem; array)
        *elem = val;
}

void main()
{
    int x, y, z;
    [&x, &y, &z].each(7);

    // Output: 7 7 7
    Stdout.formatln("{} {} {}", x, y, z);
}
-------------------------

As you can see, it's not quite was I was trying to get.

Things that would probably need to be changed to get what I really want:

- Some sort of variable reference tuple (not a type tuple like we have now), 
so we could get rid of the pointer stuff, ie: Change "each([&x, &y, &z], 
7);"  to something like "each((x, y, z), 7);"

- A *real* extention method syntax and a *real* property syntax, like in C#, 
plus the ablity to combine them, so then we could do:

-------------------------
import tango.io.Stdout;

// The "this" indicates it's an extension of T[]
// and therefore can be called with
// tuple.each instead of each(tuple)
T each(T, V)(this T() tuple)
{
    // set{}, so this is a hypothetical "extension property"
    // instead of an extention method.
    set(V val)
    {
        foreach(ref T elem; tuple)
            elem = val;
    }
}

void main()
{
    int x, y, z;
    // Notice that the "xxx.each()" and "each = yyy"
    // syntaxes no longer conflict with each other.
    (x, y, z).each = 7;

    // Output: 7 7 7
    Stdout.formatln("{} {} {}", x, y, z);
}
-------------------------





More information about the Digitalmars-d mailing list