4 ms·
I think you could actually do this in C++, but not with runtime polymorphism. I think you should be able to define extractBody and extractTextBody as template f
by scott_s 14y ago
I think you could actually do this in C++, but not with runtime polymorphism. I think you should be able to define extractBody and extractTextBody as template functions. If the signatures (return type, name and parameters) of the functions of the interface are identical - and it looks like they are - then this would work as a C++ template function. Behind the scenes, the compiler would generate two different kinds of functions for you, one for each of the types you called it with.
(Please note I'm not saying this would be better, just pointing out that it's possible.)
- pdhborges 14y agoAnd he doesn't even have to define an interface.
- scott_s 14y agoConcepts were an attempt to allow people to explicitly state what that interface is: http://en.wikipedia.org/wiki/Concepts_(C%2B%2B) http://en.wikipedia.org/wiki/Concepts_(C%2B%2B)
- plorkyeran 14y agoFor runtime polymorphism you'd have to define a wrapper of some sort, which in this specific case would be completely trivial since it's only one method. In more complex cases it gets pretty awful compared to the Go version, but it's not impossible (although AFAIK if the methods you care about have different names in the different implementations you'll need a wrapper even in Go).
- bslatkin 14y agoYes, it's possible to use C++ templates to accomplish this. But you'd still miss other features because the types of Header and MIMEHeader are not actually unified. Go would let you statically type-check at compile time, but also understand those types are the same (as "emailHeader") at runtime, so you could do this: x := ... // Header, MIMEHeader, or whatever you want v, ok := x.(emailHeader) if !ok { fmt.Println("It's not compatible with Get(string) -> string") } So, it's a nice mix of static and dynamic typing. (ps: http://golang.org/ref/spec#Type_assertions http://golang.org/ref/spec#Type_assertions)