3 ms·
Can someone please explain to me of what use generics actually are and how they create faster code or improve the typing-workflow? I'm serious, I have no idea.
by MrYellowP 5y ago
Can someone please explain to me of what use generics actually are and how they create faster code or improve the typing-workflow? I'm serious, I have no idea.
Googling it, I get:
> In a nutshell, generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. Much like the more familiar formal parameters used in method declarations, type parameters provide a way for you to re-use the same code with different inputs.
As someone who believes that OOP is bullshit and making things worse instead of better, can someone please put the above paragraph into humanly understandable words which translate into actual practical gains and improvements? Like ... actual use cases, maybe?
Thanks!
- Genmutant 5y agoAn use case that comes up quite often is building a List / Map / Set implementation. You want to have it to enforce to (typesafe) take only objects of the same type, and when taking it out again, get the object in the same type back. And you want the compiler to complain, when to try to put a String in a List<int> (a list of strings), or try to asign the object you get out of the list to a boolean. And obviously you don't want to write a specific List implementation for every Type, like a StringList, DoubleList, IntList,... especially as every method in it would basically work the same.