[DGD] 1.3.8

Felix A. Croes felix at dworkin.nl
Wed Oct 7 00:06:43 CEST 2009


Bruce Mitchener <bruce at cubik.org> wrote:

> On the off-chance that you find this interesting ... some time ago in 
> Cold, we had this feature with a couple of extensions:
>
> ==================
> Scatter
> In a scatter assignment, multiple variables are specified within square 
> brackets on the left and each element from the list expression on the 
> right is assigned to the respective variable on the left, with any 
> remaining elements in the list being discarded. For example:
>
>            [var1, var2, var3] = ["this", ["for"], 1, 'that]
>
> In this example var1 is assigned the string "this", var2 is assigned the 
> list ["for"], var3 is assigned the integer 1 and the remaining symbol 
> 'that is discarded.
>
> The remaining elements may all be grouped in a new list which is stored 
> in the last variable by using the list splice operator (@) on the last 
> variable in the scatter list, such as:
>
>            [var1, var2, @var3] = ["this", ["for"], 1, 'that]
>
> In this example var3 would have the value [1, 'that]. The list splice 
> operator may only be used this way on the last variable in the scatter list.
>
> Scatter assignments may also be nested, assuming the list expression is 
> a nested list in the same order as the scatter list. For example:
>
>            [var1, [var2, @var3], var4] = ["this", ["for", 1, 2, 3], 'that]
>
> In this example var1 is assigned "this", var2 is assigned "for", var3 is 
> assigned the new list [1, 2, 3] and var4 is assigned the remaining 
> symbol 'that.
> ==================

I considered nesting, and also other assignment operators:

    ({ a, b, c }) += ({ 1, 2, 3 });

but in the end I settled on just the simple case that helps with returning
multiple values.  Apart from that, all of these options are syntactic sugar
without anything which could not be done in LPC before, and I'm trying to
be conservative with adding new language features.

What long bothered me about returning multiple values is this:

    arr = func();
    a = arr[0];
    b = arr[1];
    c = arr[2];

or even this:

    # include "some_file_that_defines_the_return_values_of_func.h"

    arr = func();
    a = arr[FUNC_RET_A];
    b = arr[FUNC_RET_B];
    c = arr[FUNC_RET_C];

when the function returning never makes use of array indices, and thus
would not automatically reorder return values when that order is changed
in the include file.

Regards,
Felix Croes



More information about the DGD mailing list