On 20 October 2012 23:09, Michael Stowe <mikegst...@gmail.com> wrote:

> I see what you're trying to do but not a big fan of how it's being
> implemented in the example given.
>
> - Mike
>
> Sent from my iPhone
>
> On Oct 20, 2012, at 4:02 PM, Rasmus Lerdorf <ras...@lerdorf.com> wrote:
>
> > On 10/20/2012 01:59 PM, Nikita wrote:
> >> Hello, list. I want to propose generics. For those, who don't know what
> it is, here's example: say we have a Comment class, that has a method
> getBody. Also we have Collection class, that implements Traversable. Now,
> if I want to validate all insertions into collection of comments, I would
> need to create CommentCollection class, extend it from Collection, and
> override all the methods that are dealing with adding new elements to
> collection. Moreover, I can't have any hints from my IDE about type of
> collection's elements, so if I'll have code like this:
> >> foreach ($commentCollection as $comment) {
> >>    $comment->getBody();
> >> }
> >> There will be no way for IDE to know, of what type that object will be.
> >>
> >> But there's how I could solve my problem with generics:
> >> class Collection<T> implements Traversable
> >> {
> >>    ...
> >>    public function add(T $element){}
> >> }
> >> $collection = new Collection<Comment>();
> >>
> >> $collection->add(new Comment());
> >>
> >> $collection->add("that will be error");
> >>
> >> Actually, that's, again, all about type hinting. If we went so far with
> scalar type hinting, then generics would also be a good addition.
> >>
> >> So, what you think?
> >
> > Personally I would hate to see this anywhere near PHP.
> >
> > -Rasmus
> >
> >
> > --
> > PHP Internals - PHP Runtime Development Mailing List
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Well, the example just shows what I've already got used to when working
with Java.
I like it and it would be of great help to me (especially in reducing the
amount of code to be tested), but there's a lot more around that, like
multiple generic types (new Map<Comment, User>()) and how reflection would
deal with it...

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

Reply via email to