3 ms·
I broadly agree with you, so this isn't to disprove you or anything, but in case you hadn't seen it before: you can do type checks inside generic functions. You
by Groxx 2y ago
I broadly agree with you, so this isn't to disprove you or anything, but in case you hadn't seen it before: you can do type checks inside generic functions. You just have to trick the compiler / do pointless boxing because the compiler is overly simple.
This fails: https://go.dev/play/p/3J4urjOU6lc https://go.dev/play/p/3J4urjOU6lc
v, ok := thing.(target)
But this works: https://go.dev/play/p/Zb_fnAMaqZb https://go.dev/play/p/Zb_fnAMaqZb
v, ok := ((any)(thing)).(target)
It's basically because generics are generated code for specific types with little more than text replacement, and type assertion only works on interfaces, and it can't rule out non-interfaces. But if you box it in an `any`, it's fine, just like it's fine to `((any)(5)).(int)` anywhere else (or any other equivalent construct).