fix interface methods that return a value

pull/2566/head
Alexander Medvednikov 2019-11-08 07:43:44 +03:00
parent 985fb91ee8
commit 7a8e7b4db8
3 changed files with 8 additions and 5 deletions

View File

@ -389,6 +389,7 @@ fn sort_structs(types []Type) []Type {
return types_sorted return types_sorted
} }
// Generates interface table and interface indexes
fn (v &V) interface_table() string { fn (v &V) interface_table() string {
mut sb := strings.new_builder(100) mut sb := strings.new_builder(100)
for _, t in v.table.typesmap { for _, t in v.table.typesmap {

View File

@ -735,7 +735,7 @@ fn (p mut Parser) fn_call(f mut Fn, method_ph int, receiver_var, receiver_type s
p.cgen.resetln('') p.cgen.resetln('')
var := p.expr_var.name var := p.expr_var.name
iname := f.args[0].typ // Speaker iname := f.args[0].typ // Speaker
p.gen('((void (*)())(${iname}_name_table[${var}._interface_idx][$idx]))(${var}._object)') p.gen('(($f.typ (*)())(${iname}_name_table[${var}._interface_idx][$idx]))(${var}._object)')
} }
} }
//println('r=$receiver.typ RT=$receiver_type') //println('r=$receiver.typ RT=$receiver_type')

View File

@ -27,16 +27,18 @@ struct Foo {
speaker Speaker speaker Speaker
} }
fn perform_speak(s Speaker) bool { fn perform_speak(s Speaker) {
s.speak() s.speak()
return true assert true
name := s.name()
assert name == 'Dog' || name == 'Cat'
} }
fn test_perform_speak() { fn test_perform_speak() {
d := Dog{} d := Dog{}
assert perform_speak(d) perform_speak(d)
cat := Cat{} cat := Cat{}
assert perform_speak(cat) perform_speak(cat)
f := Foo { f := Foo {
//speaker: d //speaker: d
} }