5 ms·
using interface{} and reflect is good enough for the many problems of using X datatype
by dana321 8y ago
using interface{} and reflect is good enough for the many problems of using X datatype
- mixedCase 8y agoOne of Go's tennets is "clear is better than clever", followed by "reflection is never clear". So no, idiomatic Go is not about using reflection, it's what we have to resort to because the language is so limited.
- dana321 8y agoWe're talking about simple reflection here, pass in the value, infer the type and cast to that type. Most people are totally confused by generics, i personally don't have a problem using them or not. At the end of the day, its just template metaprogramming.
- aikah 8y agoNo it is not. What's the point of a compiler if you check types at runtime? It's just that a lot of things in Go weren't carefully designed and now the price of adding something to the language is the price of dealing with technical debt. For instance Go has a form of exception (panics), returning errors is purely a convention. But panics aren't very good at exceptions either.
- dana321 8y agoWell, what was the point of adding std::any in C++17? Generics aren't a cure-all, they merely duplicate code and change the type. Its not much better than copy-paste and replace the type names.
- aikah 8y ago> Generics aren't a cure-all, they merely duplicate code and change the type. Its not much better than copy-paste and replace the type names. Nobody ever said that. > . Its not much better than copy-paste and replace the type names. Yes it is. You let the compiler do the job, instead of doing it manually.
- dana321 8y agoOk, well i just said it, so somebody did. There is no difference whatsoever between the same function duplicated with different types.
- aikah 8y ago> Ok, well i just said it, so somebody did. Don't be obtuse, nobody ever said "Generics are a cure-all". > There is no difference whatsoever between the same function duplicated with different types. Yes there is, it's called polymorphism.