Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:67883 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 50636 invoked from network); 26 Jun 2013 15:24:34 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 26 Jun 2013 15:24:34 -0000 Authentication-Results: pb1.pair.com header.from=florinpatan@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=florinpatan@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.219.46 as permitted sender) X-PHP-List-Original-Sender: florinpatan@gmail.com X-Host-Fingerprint: 209.85.219.46 mail-oa0-f46.google.com Received: from [209.85.219.46] ([209.85.219.46:36880] helo=mail-oa0-f46.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 1D/84-29746-0B70BC15 for ; Wed, 26 Jun 2013 11:24:33 -0400 Received: by mail-oa0-f46.google.com with SMTP id h1so15064591oag.5 for ; Wed, 26 Jun 2013 08:24:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=JUxqT4y+G6oUrU9EOBRTv8VinEQSKATeyb8mgLA7gcE=; b=xJRijFyN5LvAR/7ev0xiUGBRVr2IB/e7buJqYlaJoXGOaYcOVCSoqkkea7UxGe4M+h pIVO7G7bajQUXc2NxpJnnxlnHhAa5y1hTZMBHoodba6ZB0RK3PMvR/71aoTC6fFy7s77 zG2f5gtOmH4RFycGJilSemLtSjfkro6WMdEoBEWlLWkKY6HDkIjO7z4LvmSGuIYzwvzY NCmwkV0X0AzqH55Th1+ibErPOUDKgTP5sMZriRExblhVxBtLFvzuUPQeVEECfhWiQQP0 zMnu9a2O0RibvpDylfCm8xy+OgwZ4VSc1vfPZJfF6f2CHTSK+KWkmcfaOFLXa9MGM4if FRUQ== X-Received: by 10.60.164.33 with SMTP id yn1mr704292oeb.5.1372260269918; Wed, 26 Jun 2013 08:24:29 -0700 (PDT) MIME-Version: 1.0 Received: by 10.76.18.161 with HTTP; Wed, 26 Jun 2013 08:23:58 -0700 (PDT) In-Reply-To: References: Date: Wed, 26 Jun 2013 17:23:58 +0200 Message-ID: To: Julien Pauli Cc: Patrick ALLAERT , Maxwell , Sherif Ramadan , "internals@lists.php.net" Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] Request for comments - new PHP feature: return typing From: florinpatan@gmail.com (Florin Patan) On Wed, Jun 26, 2013 at 3:35 PM, Julien Pauli wrote: > On Wed, Jun 26, 2013 at 2:58 PM, Patrick ALLAERT > wrote: >> >> 2013/6/26 Maxwell : >> > Sherif, >> > I have to disagree with you on this in the same way that we have type >> > hints >> > for input variables on methods, this could also be useful. >> > Consider an interface with a getter method defined. As it stands now in >> > php, they're not entirely useful, since you can't define what type of >> > object your expecting from the implementer. That greatly reduces the >> > functionality of an interface and this proposal gives solution to that >> > problem. >> > >> > Florin, >> > I think the overall concept is good, I want to see something like this >> > in >> > the future, but I think this might be the wrong approach. I don't think >> > we >> > should be hinting primitives. if we're going to have a return type hint >> > on >> > a method, I would have it match the functionality of the input type >> > hints >> > for functions we already have, where you can hint at an instance of an >> > object type, such as Iterator or StdClass, or an array, but not hint >> > primitives such as int and string, like I currently see in the draft. >> > Also, you should be aware that your proposed syntax for the return type >> > ( >> > ) is interfering with: >> > https://wiki.php.net/rfc/protocol_type_hintingand seems unnecessary to >> > me. >> >> Thanks Maxwell, you summarized very well what I was thinking about it. >> So +1 from me for the general feature at a first glance, but it should >> then be very close to what is possible with input type hinting. >> >> It makes it possible to write interfaces that acts like specifications >> and to easier the writing of: >> >> class Foo { >> public function bar($input) { >> $return = Baz::foo($input); >> if (!$return instanceof ReturnType) >> trigger_error("Cannot divide by zero", E_USER_ERROR); // >> or throwing some RuntimeException >> >> return $return; >> } >> } >> >> to: >> >> class Foo { >> public function bar($input) { >> return Baz::foo($input); >> } >> } > > > +1 for the global idea as this would help so much OO programs and framework > based programs. > > But what Sherif said proves as well the limits of the idea. PHP is not > strongly typed. So each check, should it be type hinting or return type > hinting, has to be done at runtime , which is bad for performances. > > Not talking about the type juggling which could lead the programmer as > shooting himself. > > Julien.Pauli Hi, Type juggling is explicitly not allowed in the draft and I would hope it can remain the same in the final implementation. That's actually one of the things I want to prevent. I love PHP because of type juggling which can be very handy at times but for such feature this would be a total no no. As for limited applicability, I think that frameworks could benefit greatly from this as well as systems where you really count on having the least surprise possible from code. And as more people turn to PHP to bootstrap their startup, idea or whatnot, having more code quality features would only help the language to maintain its traction and improve the life all-around. Best regards ---- Florin Patan https://github.com/dlsniper http://www.linkedin.com/in/florinpatan