Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:60583 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 11682 invoked from network); 14 May 2012 18:28:21 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 May 2012 18:28:21 -0000 Authentication-Results: pb1.pair.com header.from=smalyshev@sugarcrm.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=smalyshev@sugarcrm.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain sugarcrm.com designates 67.192.241.143 as permitted sender) X-PHP-List-Original-Sender: smalyshev@sugarcrm.com X-Host-Fingerprint: 67.192.241.143 smtp143.dfw.emailsrvr.com Linux 2.6 Received: from [67.192.241.143] ([67.192.241.143:54760] helo=smtp143.dfw.emailsrvr.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 4A/E0-03088-3CE41BF4 for ; Mon, 14 May 2012 14:28:20 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp14.relay.dfw1a.emailsrvr.com (SMTP Server) with ESMTP id B60FE29B058; Mon, 14 May 2012 14:28:16 -0400 (EDT) X-Virus-Scanned: OK Received: by smtp14.relay.dfw1a.emailsrvr.com (Authenticated sender: smalyshev-AT-sugarcrm.com) with ESMTPSA id 48D3229B0F2; Mon, 14 May 2012 14:28:16 -0400 (EDT) Message-ID: <4FB14EBF.7070404@sugarcrm.com> Date: Mon, 14 May 2012 11:28:15 -0700 Organization: SugarCRM User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:12.0) Gecko/20120428 Thunderbird/12.0.1 MIME-Version: 1.0 To: Seva Lapsha CC: "internals@lists.php.net" References: <4FB04675.6000307@sugarcrm.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [RFC] Custom Casting From: smalyshev@sugarcrm.com (Stas Malyshev) Hi! > Not quite. The proposed is a syntactic sugar which is thought to handle > any transformation of a value, not necessarily or limited to type or > class conversion. It is of course possible to limit the usage to just > that, with any user defined convention or "best practice". In fact it's > pretty distinct from primitive casting, I just had in mind that reusing > the casting syntax could be an advantage due to similarity of the behavior. > > In simple words the statements of /$var = (ClassName)$var/ or > /function(ClassName $var){}/ would not be read as "Cast to", but "Cast > with". But currently this syntax already means "convert value to a value of this type" in the first case and "allow only value of this type" in the second case. Overloading this syntax IMHO will lead to a singificant confusion, and you would not know what exactly foo(ClassName $var) means - would it only accept ClassName or would it instead convert $var using transformation ClassName? My point is exactly that these are different things with different results. > The example suggestion with wrapping the value in an object just for > handling value validation/sanitization is not just overkill, but also is > excess, since there is no any need to have the value wrapped after the > function input processing. In fact, the closest construct to the > mentioned is: > > function foo(/* to be casted with PositiveInteger */ $i) { > $i = PositiveInteger::cast($i); > return 2*sqrt($i); > } Yes, I know. These are two different approaches - the difference is where the casting responsibility lies. You can define a type PositiveInteger and assign it the responsibility or you can define that each client is responsible for its own casting, however it wants to do it. I understood that you were going for the former. I think the idea of custom casting might be useful, but overloading existing syntax with it will lead to serious confusion. -- Stanislav Malyshev, Software Architect SugarCRM: http://www.sugarcrm.com/ (408)454-6900 ext. 227