V version: V 0.1.27 847a103
OS: macOS 10.15
What did you do?
mut a := ["pop"]
a << "foo"
println(a.pop())
What did you expect to see?
```
"foo"
**What did you see instead?**
pop is not a method of array :D
I did this vlib.builtin implementation but it lacks compiler support so it can't infer the inner type and a hacky dereference cast must be done in order to get the string innstead of the unallocated pointer value.
pub fn (a mut array) pop() voidptr {
mut res := memdup(a.last(), a.element_size)
a.delete(a.len - 1)
return res
}
To do the cast right now its a pretty weak approach:
tag := *&string(x.stack.pop())
```
This API is not very natural, we should adopt a similar << approach (auto generate code). But we don't have a proper representation (including prepend insert first last...).
I have an idea, similar to ORM, defining fixed functions, such as push_back push_front pop_back pop_front prepend insert...
I like your idea @yuyi98. More clearer.
I dont know where the << syntax appear but i prefer the vala style of using meaningful names instead of overloading or aliasing operators
Imho << is fine for sending messages ala inferno in go. But in V we are using it for pushing only
In c++ its used for streams because its an overloaded operator. Also, even if we have that sugar we should still have push and pop methods
On 8 May 2020, at 17:15, yuyi notifications@github.com wrote:

This API is not very natural, we should adopt a similar << approach (auto generate code). But we don't have a proper representation (including prepend insert first last...).
I have an idea, similar to ORM, defining fixed functions, such as push_back push_front pop_back pop_front prepend insert...—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
Well... I agree with you @radare...
Also, even if we have that sugar we should still have push and pop methods
No, that'd go against the one way policy. Also we don't have 1.plus(2) just because we have 1 + 2. << is an operator for appending to an array.
Since V is similar to Go syntax, perhaps there's an argument for borrowing the Go channel syntax. Maybe something like:
mut a := ["pop"]
a << "foo"
println(<< a) // mutates a and prints "pop"
Somehow this feels like an "advanced" version of something that should also have a simple (edit: as in "more beginner friendly") syntactical representation. However, since V is focused around immutability, mutations might actually be advanced enough already and therefore maybe warrants a more "advanced" syntax?
edit again: I read println(<< a) as "pop_front" though so it's not exactly what @radare was requesting
mut arr := [1]
// imitate append()
arr << [3, 4] // [1, 3, 4]
// imitate pop()
n := 2 // number of items to operate on
arr >> n // [1]
arr >> // [] (default is 1)
// imitate prepend()
[-1, 0] >> arr // [-1, 0, 1]
// imitate shift()
2 << arr // [1]
// imitate insert()
arr = [1, 4]
[2, 3] >1< arr // [1, 2, 3, 4]
:sweat_smile:
@radare
What did you expect to see? "foo"
It should be ["pop"] actually :)
Oh, never mind, you are printing the popped value, not the array :D
Perl has
push - add to end of array
pop - remove from end of array
unshift - add to beginning of array
shift - remove from beginning of array.
It would be handy to have all 4 operations in V as well... preferably with more v-like syntax.
Ruby also has push, pop, shift, and unshift. Personally I dislike using operators
We have array.pop() now.
fn test_array_int_pop() {
mut a := [1,2,3,4,5]
assert a.len == 5
x := a.last()
y := a.pop()
assert x == y
assert a.len == 4
z := a.pop()
assert a.len == 3
assert z == 4
a.pop()
a.pop()
final := a.pop()
assert final == 1
}
Lovely!
El 19 jul 2020, a les 18:25, Swastik Baranwal notifications@github.com va escriure:

We have array.pop() now.fn test_array_int_pop() {
mut a := [1,2,3,4,5]
assert a.len == 5
x := a.last()
y := a.pop()
assert x == y
assert a.len == 4
z := a.pop()
assert a.len == 3
assert z == 4
a.pop()
a.pop()
final := a.pop()
assert final == 1
}
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
shift, and unshift
I think arr.prepend and arr.delete(0) are clearer. unshift could just return a slice arr[1..].
Or a.lpush(), a.lpop()
Most helpful comment
Since V is similar to Go syntax, perhaps there's an argument for borrowing the Go channel syntax. Maybe something like:
Somehow this feels like an "advanced" version of something that should also have a simple (edit: as in "more beginner friendly") syntactical representation. However, since V is focused around immutability, mutations might actually be advanced enough already and therefore maybe warrants a more "advanced" syntax?
edit again: I read
println(<< a)as "pop_front" though so it's not exactly what @radare was requesting