Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62137 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 73261 invoked from network); 14 Aug 2012 08:46:08 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Aug 2012 08:46:08 -0000 Authentication-Results: pb1.pair.com smtp.mail=kris.craig@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=kris.craig@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.170 as permitted sender) X-PHP-List-Original-Sender: kris.craig@gmail.com X-Host-Fingerprint: 74.125.82.170 mail-we0-f170.google.com Received: from [74.125.82.170] ([74.125.82.170:62839] helo=mail-we0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A1/34-00812-F401A205 for ; Tue, 14 Aug 2012 04:46:08 -0400 Received: by weyr1 with SMTP id r1so100278wey.29 for ; Tue, 14 Aug 2012 01:46:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=xTiuwaYb4ZbMMoNO/Zgqu3dX4CkM40C024WIJ5t7/uU=; b=MkPJJ6oEmstEAh7AO07+j2UUQ4SU2go5S4TFWjoN0MMkI/UKG0AHMUTl3xdXnl1pEk etWeclJv7yX5WCoO5lEGLTeBw99NcyyrS1nLTnTU/on0nIpd++pMWNfD30Fb+jiMw50m d70MEXKYaFYsGUQFBnbolC0enJcnBlgYxFL55cNB0TM8FeRMYgiGO+E85yJ6ZKUM5ztM UHifhGPCxqAA9yZjUnutLlovKR9/28qj09NYVdgd0GYwKpe17youAVmbVJZH+I9oCt0w 94JxLhTzncfnPfR337hMZ1RZVI2Za+twcj3qvaJvYTZkZeiQBYTfvM2rY9RB9L7DrSRB wviQ== MIME-Version: 1.0 Received: by 10.216.181.67 with SMTP id k45mr5513295wem.17.1344933965102; Tue, 14 Aug 2012 01:46:05 -0700 (PDT) Received: by 10.216.211.30 with HTTP; Tue, 14 Aug 2012 01:46:05 -0700 (PDT) In-Reply-To: References: Date: Tue, 14 Aug 2012 01:46:05 -0700 Message-ID: To: Stan Vass Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary=0016364162d53c7e1d04c735d70a Subject: Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces From: kris.craig@gmail.com (Kris Craig) --0016364162d53c7e1d04c735d70a Content-Type: text/plain; charset=ISO-8859-1 On Tue, Aug 14, 2012 at 12:46 AM, Stan Vass wrote: > I've felt the need for this for some time. > > Proposed syntax: > ------------------------- > > $x = (InterfaceName) $container->service; > > Proposed behavior: > --------------------------- > > Checks if the instance if an instance of the given class/interfaces. If > yes, does nothing, if not, issues a fatal error. Pseudo code: > > $temp = $container->service; > if ($temp instanceof InterfaceName) { > $x = $temp; > } else { > trigger_error('The object can not be cast to InterfaceName'); > } > > Use cases: > ---------------- > > As my example hints, the intent is to provide type safety and hinting for > the increasingly used dependency injection container pattern. > > It's useful to provide type safety also for all similar patterns which may > not return a result of the expected type: Registry, ServiceLocator, Factory > etc. > > Additionally to runtime type safety, this will also serve to editors/IDEs > as an inline hint to provide proper autocompletion, i.e. instead of this: > > /** @var $foo InterfaceName */ > $foo = $container->service; > > People can now just write: > > $foo = (InterfaceName) $container->service; > > Alternative syntax: > -------------------------- > > I realize "casting" isn't a perfect match as the behavior is equivalent to > inline type hinting. On the other hand, casting and inline type hinting in > dynamic languages often behave identically (see the casting behavior in > ActionScript which combines type hints with a JS-like dynamic type system). > > And this syntax doesn't introduce new keywords. Still, alternative ways to > implement this could be: > > 1. $foo = $container->service as InterfaceName; > 2. $foo = $container->service is InterfaceName; > 3. $foo = (InterfaceName $container->service); > 4. InterfaceName $foo = $container->service; > > All of those would be fine, especially 4, which has precedent in other > languages, and in PHP as well with typehinting and in catch blocks: catch > (ClassName $e) { ... } > > I'm ok with suggestions for any syntax that is as short, if it fits the > behavior better. > > Feedback? > > Stan I actually like the (InterfaceName) casting approach. I can see how type casting as an object instance could lead to all sorts of innovative coding architectures. I'm less enthused about the 4 alternatives you mentioned, though. But overall I think it's a pretty awesome idea. --Kris --0016364162d53c7e1d04c735d70a--