builtin: add arr.pop_left() func#25133
Conversation
|
Connected to Huly®: V_0.6-23654 |
|
So... same as |
Not exactly. |
|
Ah, alright. |
|
I am curious, why is it called .shift() here, and not .pop_left() as in the issue? |
which name is best? I vote for |
|
I have no opinion on the name. If there is an existing precedent for .shift(), fine. |
// free frees all memory occupied by the array.
@[unsafe]
pub fn (a &array) free() {
$if prealloc {
return
}
// if a.is_slice {
// return
// }
if a.flags.has(.nofree) {
return
}
mblock_ptr := &u8(u64(a.data) - u64(a.offset))
if mblock_ptr != unsafe { nil } {
unsafe { free(mblock_ptr) }
}
unsafe {
a.data = nil
a.offset = 0
}
}Is the |
|
No one updates .offset except the .slice method (for creating a new slice view) and now the .shift one (which in essence also creates a new slice + returning the first element of the old). |
Yes, you are right. |
|
(i.e. the purpose of the .offset field, is that in combination with .data, you can always get back to the original memory block start, even when creating multiple slices/views to the same elements) |
|
So...
Basically, there is no "standard" way of doing this. |
|
|
|
|
I think that pop_front or pop_left may be more descriptive and less confusing (but I am fine with shift too). |
|
To me, Of course, then you run into... At any rate, as long as it's documented, it doesn't matter that much. It's more a cognitive thing... what would make the most sense if you didn't know what was there, and you were searching for it? Last... it seems like it wouldn't hurt anything to just make |
|
no, delete has a different semantic (and implementation) compared to this method |
yes, you are shifting the start of the array, |
The intention is that this method can be used in the implementation of a queue (avoiding the copying/movement that delete(x) does). |
I agree that documentation can help with clearing the meaning and behavior. The name decision at this point boils down to a balance between mnemonics and clarity. |
|
I tend to prefer i.e. my preference is currently in this order:
|
|
|
|
OK, I will change |
spytheman
left a comment
There was a problem hiding this comment.
Excellent work @kbkpbot .
I've asked @medvednikov on Discord, and he agrees with the pop_left() name too.
Feature require by #24591
Just like
array.pop(),array.pop_left()act likeret=array.first() array.delete(0)