A few times I've done an interview and I'll be ask...
# random
c
A few times I've done an interview and I'll be asked about generics. What they're used for. When do I use them. etc. Honestly... I feel like I don't implement generics all that often, but when I do they're insanely helpful. Of course I use generics with collections api, etc. But "why are generics useful" is hard for me to put into words. How do you all feel about my answer "generics are a way to get type safety in situations where you want to downcast the type so that you can use many different types"?
s
When I'm using generics in my own code, I think the answer generally boils down to this: the code before my function and the code after my function both need to know the specific type of a value, but my function itself handles the value without needing to know its specific type
"Downcast" might not be the right word to use, but I think I get where you're coming from with it
❤️ 1
💯 1
y
I mean you can always go the functional route to sound "hip" and talk about Functors (classes with a generic type variable e.g.
List
) and Parametric Polymorphism (generic functions). I do personally think of it as a way to get extra type safety.