Re: Generics proposal
| From: | Nikita Nefedov | Date: | Sun, 21 Oct 2012 11:33:56 +0000 |
| Subject: | Re: Generics proposal | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-63579@lists.php.net to get a copy of this message | ||
No, this is useful in any OOP-language where there is such thing as type, and people need to validate types.
I unerstand what you are saying about PHP being an easy language, but in opinion if you don't want to use that feature you can not use it, as you can not use typehinting in functions for example.
Hi Nikita, What you're asking for is useful in other languages, but it doesn't match the style of PHP which is to be a quick and easy type-less scripting language. What you're proposing is clearly more suited to something like Java or C#, but doesn't really belong here. I'd also hate to see this anywhere near PHP. Many thanks, Paul. On Sat, Oct 20, 2012 at 11:10 PM, Marco Pivetta <ocramius@gmail.com> wrote:On 20 October 2012 23:09, Michael Stowe <mikegstowe@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 <rasmus@lerdorf.com>wrote:On 10/20/2012 01:59 PM, Nikita wrote:it is, here's example: say we have a Comment class, that has a method getBody. Also we have Collection class, that implements Traversable.whatHello, list. I want to propose generics. For those, who don't knowNow,if I want to validate all insertions into collection of comments, Iwouldneed 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:scalar type hinting, then generics would also be a good addition.be.foreach ($commentCollection as $comment) { $comment->getBody(); } There will be no way for IDE to know, of what type that object willwithBut 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-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.phpPersonally 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.phpSo, what you think?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/