cgen: handle `is` for interfaces
parent
b233c24d19
commit
2369a5c8c6
|
@ -3260,7 +3260,12 @@ fn (mut g Gen) is_expr(node ast.InfixExpr) {
|
|||
} else {
|
||||
g.write('.')
|
||||
}
|
||||
sym := g.table.get_type_symbol(node.left_type)
|
||||
if sym.kind == .interface_ {
|
||||
g.write('_interface_idx == ')
|
||||
} else if sym.kind == .sum_type {
|
||||
g.write('typ == ')
|
||||
}
|
||||
g.expr(node.right)
|
||||
}
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@ fn (c Cat) name_detailed(pet_name string) string {
|
|||
return '$pet_name the ${typeof(c)}, breed:${c.breed}'
|
||||
}
|
||||
|
||||
fn (c mut Cat) set_breed(new string) {
|
||||
fn (mut c Cat) set_breed(new string) {
|
||||
c.breed = new
|
||||
}
|
||||
|
||||
|
@ -49,12 +49,11 @@ fn (d Dog) name_detailed(pet_name string) string {
|
|||
return '$pet_name the ${typeof(d)}, breed:${d.breed}'
|
||||
}
|
||||
|
||||
fn (d mut Dog) set_breed(new string) {
|
||||
fn (mut d Dog) set_breed(new string) {
|
||||
println('Nah')
|
||||
}
|
||||
|
||||
// do not add to Dog the utility function 'str', so the default one will be used, as a sample
|
||||
|
||||
fn test_todo() {
|
||||
if true {
|
||||
} else {
|
||||
|
@ -66,9 +65,9 @@ fn perform_speak(a Animal) {
|
|||
assert true
|
||||
name := a.name()
|
||||
assert name == 'Dog' || name == 'Cat'
|
||||
// if a is Dog {
|
||||
// assert name == 'Dog'
|
||||
// }
|
||||
if a is Dog {
|
||||
assert name == 'Dog'
|
||||
}
|
||||
println(a.name())
|
||||
println('Got animal of type: ${typeof(a)}') // TODO: get implementation type (if possible)
|
||||
// assert a is Dog || a is Cat // TODO: enable when available
|
||||
|
@ -151,7 +150,7 @@ fn test_perform_name_detailed() {
|
|||
perform_speak(cat_persian2)
|
||||
cat_persian2_str := cat_persian2.str()
|
||||
println("Persian Cat 2: '$cat_persian2_str' ...")
|
||||
assert cat_persian2_str == "Custom string conversion for Cat: Persian"
|
||||
assert cat_persian2_str == 'Custom string conversion for Cat: Persian'
|
||||
println('Test (dummy/empty) on array of animals ...')
|
||||
handle_animals([dog, cat])
|
||||
handle_animals_mutable([dog, cat])
|
||||
|
|
Loading…
Reference in New Issue