v icon indicating copy to clipboard operation
v copied to clipboard

Not initialized member variable throw interface

Open kyubuns opened this issue 7 years ago • 2 comments

struct Dog {
	a int
}

fn (d Dog) speak() int {
	println('$d.a')
	return 12345 // dummy
}

interface Speaker {
        speak() int
}

fn perform(s Speaker) {
	s.speak()
}

fn main() {
	dog := Dog{}
	println('$dog.a') // -> 0
	perform(dog)
	println('$dog.a') // -> 0
}

Actually

0
0
0

In fact

0
-375720792 // random
0

on 0.0.7

kyubuns avatar May 08 '19 10:05 kyubuns

Thanks. A minor note: you can do println(d.a) instead of println('$d.a').

medvednikov avatar May 08 '19 21:05 medvednikov

i can't understand what interface use for. i can't pass var to it, so, this interface speak() just call struct's method without any var. i not study cpp, i think it like cpp's polymorphism. but,interface don't pass variable, how i can use it? need someone give me some examples to study.

MDZ2 avatar Jul 12 '19 14:07 MDZ2