When would you not use Generic Collections?

前端 未结 8 1816
醉酒成梦
醉酒成梦 2021-01-19 00:57

The advantage of using generics is that it increases the type safety - you can only put in the correct type of thing, and you get out the correct type without requiring a ca

8条回答
  •  面向向阳花
    2021-01-19 01:42

    Generics are almost always the right thing to use. Note that languages like Haskell and ML essentially only allow that model: there is no default "object" or "void*" in those languages at all.

    The only reasons I might not use generics are:

    1. When the appropriate type is simply not known at compile time. Things like deserializing objects, or instantiating objects through reflection.

    2. When the users that will be using my code aren't familiar with them (yet). Not all engineers are comfortable using them, especially in some more advanced patterns like the CRTP.

提交回复
热议问题