Created
October 29, 2011 11:36
-
-
Save iansmith/1324357 to your computer and use it in GitHub Desktop.
suprising for a naive Java or C++ developer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// see https://gist.github.com/1324737 for a java version of the same issue, which does work | |
type fruit interface { | |
foo() int | |
} | |
type apple interface { | |
fruit | |
bar() int | |
} | |
func needFruit(f fruit) { | |
} | |
func needManyFruits(f []fruit) { | |
} | |
func f() { | |
var f fruit | |
var a apple | |
needFruit(f) | |
needFruit(a) //duck typing plus the "borrowing" of interface fruit into apple works like inheritance | |
var fs []fruit | |
var as []apple | |
needManyFruits(fs) | |
needManyFruits(as) //this doesn't work! a naive C++ or java developer is surprised! | |
//there is a hint of an explanation here: http://golang.org/doc/go_faq.html#convert_slice_of_interface | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment