vyper icon indicating copy to clipboard operation
vyper copied to clipboard

methods for modifying dynamic arrays

Open charles-cooper opened this issue 3 years ago • 6 comments

current master only has assignment between dynamic arrays, but people will expect to be able to modify arrays. we should implement modifier methods. to be familiar for python users:

method behavior
.append() add an element to an array. (analogous to solidity push(), but it changes the runtime length of the array)
.pop() pop the last element of an array. (analogous to solidity pop() but it changes the runtime length of the array
(?).extend() (tent.) concat two arrays
(?).pop(ix=?) (tent.) delete an element from the middle of an array

charles-cooper avatar Jan 21 '22 17:01 charles-cooper

I would like to also suggest a clear method (or whatever you think it should be called).

haltman-at avatar Apr 21 '22 08:04 haltman-at

I would like to also suggest a clear method (or whatever you think it should be called).

What would the semantics of this method be?

charles-cooper avatar Apr 21 '22 08:04 charles-cooper

Ah, sorry. I mean a method that would remove all elements from the given array, resetting its length to 0.

haltman-at avatar Apr 21 '22 08:04 haltman-at

Sorry, I guess it would be presumably be named whatever the analogous thing is in Python; I just suggested the first name that came to mind, I didn't think about what the naming convention here is!

haltman-at avatar Apr 21 '22 08:04 haltman-at

You can set the length of a dynamic array to zero by either using an empty list literal or using the special empty function

my_array: DynArray[uint256, 7] = [ 1,2,3]
my_array.pop()
my_array = [] # set length to zero
my_array = empty(DynArray[uint256,7]) # same thing

charles-cooper avatar Apr 21 '22 08:04 charles-cooper

Oh, sorry, I had no idea all that was legal! Disregard my request, then. Thanks!

haltman-at avatar Apr 21 '22 09:04 haltman-at