diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 2564faf2b3b864..3bd17302960c6e 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -1055,11 +1055,13 @@ fn (mut g Gen) gen_array_method_call(node ast.CallExpr, left_type ast.Type) bool g.gen_array_all(node) return true } - 'delete', 'drop' { + 'delete', 'drop', 'delete_last' { g.write('array_${node.name}(') g.gen_arg_from_type(left_type, node.left) - g.write(', ') - g.expr(node.args[0].expr) + if node.name != 'delete_last' { + g.write(', ') + g.expr(node.args[0].expr) + } g.write(')') return true } diff --git a/vlib/v/tests/array_delete_last_test.v b/vlib/v/tests/array_delete_last_test.v new file mode 100644 index 00000000000000..0fe8c796e745d2 --- /dev/null +++ b/vlib/v/tests/array_delete_last_test.v @@ -0,0 +1,55 @@ +struct ArrayList[T] { +mut: + size usize + items []&T +} + +fn new_array_list[T]() ArrayList[T] { + return ArrayList[T]{} +} + +fn (li ArrayList[T]) is_empty() bool { + return li.size == 0 +} + +fn (mut li ArrayList[T]) add(item &T) { + li.items << item + li.size += 1 +} + +fn (mut li ArrayList[T]) pop() ?&T { + if li.is_empty() { + return none + } else { + item := li.items.last() + li.items.delete_last() + li.size -= 1 + return item + } +} + +struct Student { + name string +mut: + age u8 +} + +fn (mut s Student) grow() { + s.age += 1 +} + +fn test_main() { + mut list := new_array_list[Student]() + assert list.is_empty() && list.size == 0 + + mut stu := Student{'Tom', 16} + list.add(&stu) + assert !list.is_empty() && list.size == 1 + stu.grow() + assert stu.age == 17 + + tom := list.pop()? + assert list.is_empty() && list.size == 0 + assert tom == stu + assert tom.age == 17 +}