interfaces: fix `is`

pull/5115/head
Alexander Medvednikov 2020-05-29 05:54:50 +02:00
parent b3f2c629c6
commit bec3e07635
2 changed files with 21 additions and 3 deletions

View File

@ -3614,6 +3614,11 @@ fn (mut g Gen) is_expr(node ast.InfixExpr) {
sym := g.table.get_type_symbol(node.left_type)
if sym.kind == .interface_ {
g.write('_interface_idx == ')
// `_Animal_Dog_index`
sub_type := node.right as ast.Type
sub_sym := g.table.get_type_symbol(sub_type.typ)
g.write('_${sym.name}_${sub_sym.name}_index')
return
} else if sym.kind == .sum_type {
g.write('typ == ')
}

View File

@ -225,3 +225,16 @@ fn test_interface_ptr_array() {
assert true
assert animals.len == 3
}
fn test_is() {
dog := Dog{}
assert foo2(dog) == 1
}
fn foo2(a Animal) int {
if a is Dog {
return 1
} else {
return 0
}
}