Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:63600 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 92803 invoked from network); 22 Oct 2012 16:26:32 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Oct 2012 16:26:32 -0000 Authentication-Results: pb1.pair.com smtp.mail=julienpauli@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=julienpauli@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.219.42 as permitted sender) X-PHP-List-Original-Sender: julienpauli@gmail.com X-Host-Fingerprint: 209.85.219.42 mail-oa0-f42.google.com Received: from [209.85.219.42] ([209.85.219.42:43774] helo=mail-oa0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id CE/76-52508-7B375805 for ; Mon, 22 Oct 2012 12:26:31 -0400 Received: by mail-oa0-f42.google.com with SMTP id j1so2902454oag.29 for ; Mon, 22 Oct 2012 09:26:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type :content-transfer-encoding; bh=glHM8kuZALa23BWX7G1cLWsoaCg32Ls0e4jM/9ZuX1M=; b=hW4QoskTnoTrAzpVFhPBeKjGxByqKfsWacBXdedkUVBM6n81AYVpszpwh66ZSPrK9+ +8w29nW1rLem9TWtkWz098Fl9M/G7PoYwpfulqIG7S7sSlw/9uf9dmmzPA3JWzBs7rD9 5aQH2v0H9HDi2GS4UBQRV8O5/3PMbL/MdGoJ/ndcARYPXai4bf7qxB/KV4Q+8wtqktHI kkYtweYNea8nWcrZN3LAK7LtWYGEtzALC9r0E4/Hs38hDe6DP4hUTG0z3f52E79pDMaL CY5g2dQj6Oy6gBUOSZtjC+gKWBeD+T7hve8hm3ham/22HZ3Pgrq9O+kRvWr5AhDEG2PK P2zw== Received: by 10.182.177.7 with SMTP id cm7mr7545316obc.20.1350923188807; Mon, 22 Oct 2012 09:26:28 -0700 (PDT) MIME-Version: 1.0 Sender: julienpauli@gmail.com Received: by 10.76.75.105 with HTTP; Mon, 22 Oct 2012 09:25:48 -0700 (PDT) In-Reply-To: <712F4499-949C-4C6F-A142-736A1B156189@gmail.com> References: <712F4499-949C-4C6F-A142-736A1B156189@gmail.com> Date: Mon, 22 Oct 2012 18:25:48 +0200 X-Google-Sender-Auth: jjSjInl5PLXd6enhmtwLRwwG2zA Message-ID: To: Nikita Cc: "internals@lists.php.net" Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Generics proposal From: jpauli@php.net (jpauli) On Sat, Oct 20, 2012 at 10:59 PM, Nikita wrote: > Hello, list. I want to propose generics. For those, who don't know what i= t is, here's example: say we have a Comment class, that has a method getBod= y. Also we have Collection class, that implements Traversable. Now, if I wa= nt 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. Moreo= ver, 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 implements Traversable > { > ... > public function add(T $element){} > } > $collection =3D new Collection(); > > $collection->add(new Comment()); > > $collection->add("that will be error"); > > Actually, that's, again, all about type hinting. If we went so far with s= calar type hinting, then generics would also be a good addition. > > So, what you think? Hah, I had the use case often, and every OO programmer has met this use case several time I think : Here is an example with SplObjectStorage (which is a good starting point structure for that) : type =3D $type; } public function add($o) { if (!$o instanceof $this->type) { throw new \RunTimeException("type {$this->type} expected"); } /* do something with $o*/ } } class Bar { } class Baz { } $f =3D new Foo('bar'); $f->add(new Bar); /* OK */ $f->add(new Baz); /* KO */ $f->add(array()); /* KO */ ?> IMO : this is something the language should take care of, not the developper, but that's my opinion :)