Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62217 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 47442 invoked from network); 16 Aug 2012 07:36:53 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Aug 2012 07:36:53 -0000 Authentication-Results: pb1.pair.com header.from=sebastian.krebs.berlin@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=krebs.seb@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.83.42 as permitted sender) X-PHP-List-Original-Sender: krebs.seb@gmail.com X-Host-Fingerprint: 74.125.83.42 mail-ee0-f42.google.com Received: from [74.125.83.42] ([74.125.83.42:54263] helo=mail-ee0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 4E/CC-08779-413AC205 for ; Thu, 16 Aug 2012 03:36:52 -0400 Received: by eekb15 with SMTP id b15so662255eek.29 for ; Thu, 16 Aug 2012 00:36:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:x-google-sender-delegation:in-reply-to :references:date:x-google-sender-auth:message-id:subject:from:to :content-type; bh=/Dawh6hFxK1qpyL0ky/pugYoV4dyaovCpqibp3wHMw0=; b=Yrpg7ZURlQ0R9y4QOiWfA/L1Rz1OMdtGViRUZsbye4ounJtyMlag0MbPY4wlWHPDrs w+Flz6Cfhsw2Eu/8+LUO5VKEktwaPbK81UL9zZKFnpIJJhR4p6SSy0/cPQlJNbfFcibY X3PqtfPkip4TlmIwjisKxtgf2IcbnuTsYWH0Vmr/pURGYxUwlZaF+q/AEPkbBBpashmV mp1dMG/8X8b803fh4SjUJ6MHYNaX82DG0M5hukrGVgAOpws2sHbn1uovY/bZebP3DqzE YKTuPGvI5huuSsoAS8mDF00Y1vuILSZGdls9f5WXna5faQgWcjfJGs0PcOjl66uzTrgp oSGA== MIME-Version: 1.0 Received: by 10.14.204.200 with SMTP id h48mr373739eeo.7.1345102609455; Thu, 16 Aug 2012 00:36:49 -0700 (PDT) Sender: sebastian.krebs.berlin@gmail.com X-Google-Sender-Delegation: sebastian.krebs.berlin@gmail.com Received: by 10.14.176.73 with HTTP; Thu, 16 Aug 2012 00:36:49 -0700 (PDT) In-Reply-To: References: <502A86AA.2030203@sugarcrm.com> Date: Thu, 16 Aug 2012 09:36:49 +0200 X-Google-Sender-Auth: 34zFhaQbtaBeRyrerAegV3yHJds Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary=047d7b34395839195204c75d1bed Subject: Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces From: krebs.seb@gmail.com (Sebastian Krebs) --047d7b34395839195204c75d1bed Content-Type: text/plain; charset=ISO-8859-1 Hi, after reading this mail: Is it just me or is a userspace implementation really that trivial? function ensure($object, $class) { if ($object instanceof $class) { return $object; } throw new InvalidArgumentException(sprintf('Object is not of type %s', $class)); } And then instead of all a new syntax $myObject = ensure($another->foobar, MyType::class); // [1] Did I miss something. Regards, Sebastian [1] There was another RFC that suggested this special property. 2012/8/15 Nikita Popov > On Wed, Aug 15, 2012 at 8:15 PM, Kris Craig wrote: > > On Wed, Aug 15, 2012 at 4:48 AM, Anthony Ferrara >wrote: > > > >> Stan, > >> > >> On Wed, Aug 15, 2012 at 3:57 AM, Stan Vass > wrote: > >> > >> > Hi! > >> >> > >> >> I agree with you. The one case where this syntax may be very useful > is > >> if > >> >>> we want to implement class casting. So introduce a pair of magic > >> methods > >> >>> > >> >> > >> >> I do not think we want to implement class casting. I'm not sure how > >> >> class casting even makes sense - if the object is of one class, how > can > >> >> you just make it into another class by casting? If you mean "casting" > >> >> actually returns another object of different class, then just make a > >> >> method for that that returns that object, I do not see how obscuring > the > >> >> purpose of this operation with unobvious syntax would help. > >> >> > >> > > >> > The discussion is starting to drift very far from my original > proposal. > >> > > >> > Instead of trying to guess what I mean, can't people just refer to my > >> very > >> > simple definitive proposed behavior? > >> > > >> > >> My point was that what I posted was the only way that I can see for the > >> original proposal to be useful. > >> > >> Anthony > >> > > > > Though I'm clearly in the minority on this, I for one think this proposal > > does have more merit than is being argued. There seems to be general > > agreement all around that this would provide a benefit as it pertains to > > code readability-- Not just by humans, but theoretically by doc/etc > parsers > > as well. > > > > This is where we get into arbitrary, subjective territory. To me, that > > benefit in and of itself is sufficient to warrant this feature. To many > of > > you, it is not enough. > > > > The tie-breaker for me is the fact that, though the benefits are modest, > > there's really no noticeable cost, either. The argument seems to, > > essentially, break down as follows: "This feature isn't worth our time." > > .... "Yes, it is!" .... "No, it isn't." > > Every feature has a cost, even if that cost is just maintaining the > code. Doing language changes for minority use cases, which already > have sensible solutions, doesn't make much sense. > > Another aspect here is that there is no reasonable syntax for this > feature, at least I can't think of one: > > * The syntax `$foo = (InterfaceName) $container->service` is > completely out of question. It looks like a cast, but wouldn't > actually do a cast. > * Same is to be said about `InterfaceName $foo = > $container->service`. This syntax implies that the $foo variable will > always be of type InterfaceName, even if it is later reassigned. It's > not a sensible syntax for a one time validation > * The other three syntaxes that were mentioned were just as unclear. > E.g. `$foo = $container->service as InterfaceName` again looks like a > strange cast syntax and `$foo = $container->service is InterfaceName` > looks like the assignment should evaluate to a boolean (i.e. `is` is > some kind of `instanceof`). > > On the other hand, the current ways of accomplishing the same goal are > well-established and easy to understand: > > * Using a docblock: /** @var $foo IntefaceName **/ > * Using an assertion: assert($foo instanceof InterfaceName). > > I think that the assertion is a rather concise and clear way to do > this. It is much more obvious than some new and obscure `$foo = > (InterfaceName $container->service)` syntax. > > Nikita > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --047d7b34395839195204c75d1bed--