Skip to content

Commit

Permalink
fix interface methods that return a value
Browse files Browse the repository at this point in the history
  • Loading branch information
medvednikov committed Nov 8, 2019
1 parent 985fb91 commit 7a8e7b4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions vlib/compiler/cgen.v
Expand Up @@ -389,6 +389,7 @@ fn sort_structs(types []Type) []Type {
return types_sorted
}

// Generates interface table and interface indexes
fn (v &V) interface_table() string {
mut sb := strings.new_builder(100)
for _, t in v.table.typesmap {
Expand Down
2 changes: 1 addition & 1 deletion vlib/compiler/fn.v
Expand Up @@ -735,7 +735,7 @@ fn (p mut Parser) fn_call(f mut Fn, method_ph int, receiver_var, receiver_type s
p.cgen.resetln('')
var := p.expr_var.name
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')
Expand Down
10 changes: 6 additions & 4 deletions vlib/compiler/tests/interface_test.v
Expand Up @@ -27,16 +27,18 @@ struct Foo {
speaker Speaker
}

fn perform_speak(s Speaker) bool {
fn perform_speak(s Speaker) {
s.speak()
return true
assert true
name := s.name()
assert name == 'Dog' || name == 'Cat'
}

fn test_perform_speak() {
d := Dog{}
assert perform_speak(d)
perform_speak(d)
cat := Cat{}
assert perform_speak(cat)
perform_speak(cat)
f := Foo {
//speaker: d
}
Expand Down

0 comments on commit 7a8e7b4

Please sign in to comment.