-
Notifications
You must be signed in to change notification settings - Fork 8k
Description
Description
- Of course this proposal may be merged with any other collection suggestions or something
Actually Generics may be used only in two possible ways:
-
Create Concrete (example: repository) that implements Abstract one, and concrete has some public methods that is required only inside this concrete repository, but repository calling or creation - it is responsibility of some data manager. The case differs from primary OOP idea just because every of that repositories most of the time "extended" (i mean - fixed, edited, rewritten) hundreds of times, and creating 100 interfaces for each feature with transformation method for certain repository is a useless work.
So, you create manager, implement method "getRepository() : RepositoryInterface" and then you always needed to check "is it my repository or other one".
Currently may be solved with PHPDoc@template
syntax very well, but requires additional argument in method signature to defineclass-string
$classT
to work enough good. -
And second, more often, case - is to create plain list of any other type... Common pain here is to check that list using foreach everywhere is necceseary. If you just create "typehint" for "array of something" - it kinda work, but still at the low level it will be foreach-validation everywhere, just without writing code.
So suggested stuff is to create internal class that represent "list" or maybe ok, associative, that can contain only one data type that MUST be declared. So you createnew List<string>
and typehint just check is passed value an instance ofList<string>
via simple if. Potential of this improvement is uncountable (just one example - return type "array of callables")
*3. other rarely use cases that could be realised using previous two, generics-from-generics-from-generics stuff - is about bad code smell (like intersection types - while union types usually give you more options, intersection types are just lazy checking to hide a bad code smell), i think we have to follow here "easiest-solution-less-bugs"