v.gen.c: fix generics array delete (#11429)

pull/11438/head
yuyi 2021-09-08 01:40:21 +08:00 committed by GitHub
parent ddc62ab0d3
commit bee5e7de03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 0 deletions

View File

@ -749,6 +749,18 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
g.expr(node.args[0].expr)
g.write('})')
return
} else if left_sym.kind == .array && node.name == 'delete' {
g.write('array_delete(')
if node.left_type.is_ptr() {
g.expr(node.left)
} else {
g.write('&')
g.expr(node.left)
}
g.write(', ')
g.expr(node.args[0].expr)
g.write(')')
return
}
if left_sym.kind in [.sum_type, .interface_] {

View File

@ -0,0 +1,48 @@
struct Set<T> {
mut:
field []T
}
fn (mut s Set<T>) add<T>(value T) bool {
mut result := false
if value !in s.field {
s.field << value
result = true
}
return result
}
fn (mut s Set<T>) remove<T>(value T) bool {
mut result := false
if value in s.field {
ndx := s.field.index(value)
s.field.delete(ndx)
result = true
}
return result
}
fn test_generics_array_delete() {
mut set := Set<int>{}
mut added := set.add(4)
println(added)
assert added
added = set.add(3)
println(added)
assert added
added = set.add(3)
println(added)
assert !added
println(set)
mut removed := set.remove(4)
println(removed)
assert removed
}