poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [RFC] Syntax for appending elements to arrays


From: Mohammad-Reza Nabipoor
Subject: Re: [RFC] Syntax for appending elements to arrays
Date: Thu, 26 Nov 2020 23:52:17 +0330

Hi, Jose.

On Thu, Nov 26, 2020 at 09:34:52AM +0100, Jose E. Marchesi wrote:
> 
> I just realized 2) is unfortunately ambiguous with the existing
> array-concatenation operation.  If you have:
> 
>   var a = [[1,2],[3,4]];
> 
> And you do:
> 
>   a += [5,6];
> 
> Are you actually trying to concatenate the arrays [[1,2],[3,4]] and
> [5,6], in which case you want a syntax error, or
> 
> Are you actually tring to append an element [5,6]?
> 
> If no one comes with some sane syntax for this, we will have to resort
> to the usual notation:
> 
>   a.push ([5,6]) or,
>   a.append ([5,6])
> 
> But I would really want to avoid that if possible..
> 
> No more ideas, anyone?
> 


Despite the fact I may miss something obvious, I try to explain my approach
to deal with`lhs += rhs` in pseudo-code:

    if (type (lhs) == ARRAY)
    {
        if (arr_elem_type (lhs) == type (rhs))
            arr_append_elem (lhs, rhs);
        else if (type (rhs) == ARRAY &&
                 arr_elem_type (lhs) == arr_elem_type (rhs))
            arr_concat (lhs, rhs);
        else
            error ();
    }
    /* ... */


So, here we will have:

```poke
var a = [[1,2],[3,4]];

a += [5,6];
assert (a == [[1,2],[3,4],[5,6]]);
```

This approach is also backward-compatible with current Poke codes.

But if this is too complicated to implement or understand, introducing an
`append` function is not that bad.
Also having an `extend` function is desirable.

```poke
a = [1,2,3];
b = [4,5];

a.extend(b);
assert (a == [1,2,3,4,5]);
```


Regards,
Mohammad-Reza


reply via email to

[Prev in Thread] Current Thread [Next in Thread]