adding an element to an empty array

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

adding an element to an empty array

abdelghani ALIDRA
Hi guys,

Is there any way to add an element to an empty array (must not use new: anElement)?

Cheers
Reply | Threaded
Open this post in threaded view
|

Re: adding an element to an empty array

Henrik Sperre Johansen

On 29 Jun 2015, at 2:18 , abdelghani ALIDRA <[hidden email]> wrote:

Hi guys,

Is there any way to add an element to an empty array (must not use new: anElement)?

Cheers

It sounds like you want to use an OrderedCollection, not an Array.
Those will grow and shrink as needed when you use add:/remove: calls.

Cheers,
Henry
Reply | Threaded
Open this post in threaded view
|

Re: adding an element to an empty array

Peter Uhnak
Or if you want to create an array with elements already in it (this works both for array and collections) you can also do

~~~~~~~~~~
{ anElement } "dynamic array syntax, Pharo-only"
~~~~~~~~~~
or
~~~~~~~~
#('element') "static array, works only for scalars"
~~~~~~~~
or
~~~~~~~~~~~~~~~~
Array with: anElement "constructor method, works also with more elements... Array class>>with:with:with:..."
~~~~~~~~~~~~~~~~
or
~~~~~~~~~~~~~~~~~~~~~~~~~
OrderedCollection with: anElement "as above, but creates a collection instead"
~~~~~~~~~~~~~~~~~~~~~~~~~

Peter

On Mon, Jun 29, 2015 at 2:23 PM, Henrik Johansen <[hidden email]> wrote:

On 29 Jun 2015, at 2:18 , abdelghani ALIDRA <[hidden email]> wrote:

Hi guys,

Is there any way to add an element to an empty array (must not use new: anElement)?

Cheers

It sounds like you want to use an OrderedCollection, not an Array.
Those will grow and shrink as needed when you use add:/remove: calls.

Cheers,
Henry

Reply | Threaded
Open this post in threaded view
|

Re: adding an element to an empty array

Sven Van Caekenberghe-2
In reply to this post by Henrik Sperre Johansen

> On 29 Jun 2015, at 14:23, Henrik Johansen <[hidden email]> wrote:
>
>
>> On 29 Jun 2015, at 2:18 , abdelghani ALIDRA <[hidden email]> wrote:
>>
>> Hi guys,
>>
>> Is there any way to add an element to an empty array (must not use new: anElement)?
>>
>> Cheers
>
> It sounds like you want to use an OrderedCollection, not an Array.
> Those will grow and shrink as needed when you use add:/remove: calls.

That being true, there are actually #copyWith: and #copyWithFirst: that will do the trick, although inefficiently.

> Cheers,
> Henry