3 ms·
interface{} is completely different, in that it doesn't allow polymorphism. In python, I can add = lambda a, b: a+b, and then call add on ints, floats, strs, o
by earenndil 8y ago
interface{} is completely different, in that it doesn't allow polymorphism. In python, I can add = lambda a, b: a+b, and then call add on ints, floats, strs, or some custom type of my own. In go, I can pass an interface{} and check if its type is the same as some other type I know about. But no polymorphism.
- klodolph 8y agoThis is factually incorrect, interface{} allows polymorphism. Maybe you are unaware of how interface{} works but if you want to call a method on it you just do this: func myFunc(a interface{}) { a.(interface{ f() }).f() } This works for any type "a" which has a method "f" with the correct signature, and this is what I meant when I said “except it requires casting”. The fact that you can’t do this with + is merely a consequence of the fact that + is just the __add__ method in Python, and in Golang + is not a method. The fact that Golang matches method signatures whereas Python only matches method names is not really a substantial difference in my eyes. I don’t think there’s a strong case to be made here that interface{} is substantially different from Any in Python, again, except for the required cast. And I think we all agree that neither Python nor Golang support generics. (Python supports metaprogramming, and you can implement something similar to generics with metaprogramming, but I don’t consider that to mean that Python supports generics. Type checkers retrofitted onto Python also support generics, but I don’t really consider those to be core Python yet.)
- weberc2 8y agoYou don’t have to check the types, you can say “unpack these variables as into and if they aren’t into, blow up” which is what Python does as well. The major difference is that it’s implicit in Python and explicit in Go.