Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81014 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 25420 invoked from network); 23 Jan 2015 03:02:27 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Jan 2015 03:02:27 -0000 Authentication-Results: pb1.pair.com smtp.mail=yohgaki@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=yohgaki@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.192.46 as permitted sender) X-PHP-List-Original-Sender: yohgaki@gmail.com X-Host-Fingerprint: 209.85.192.46 mail-qg0-f46.google.com Received: from [209.85.192.46] ([209.85.192.46:61298] helo=mail-qg0-f46.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id CF/1D-61273-2C9B1C45 for ; Thu, 22 Jan 2015 22:02:26 -0500 Received: by mail-qg0-f46.google.com with SMTP id i50so4373955qgf.5 for ; Thu, 22 Jan 2015 19:02:23 -0800 (PST) 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:message-id :subject:to:cc:content-type; bh=2xtHEVce34Bhqb3zhhcncBo+SetLwhbZwL0MNkCF/4g=; b=x6DuyYJPPQ6iU0aEBnL6EV8UD8AGUA0bBReWpVc4A3YYHZSqzm2KlgKRCvAFPDTToD P5vI/VoAQWWllVOemio4PlXUOP7rg6jxzW75OQR/jWQs2UlvfAaERLhKGQBuhHuQK02r P05lLBklyNNnbdEUGD8mUo01+uyNrCmD+hBdlG8/cILwOFTTRh+w8phV6+3GxlPBnFgW lwAsTPQHSoqIAUIZQXJvc97OTZOhYbnb67wHawRVVyDc/LtZBQxWTppuuVW7Jlg5Or/C wW+rciRqvJN7zYr6+33+zYWCP2nJQUaU6o5Lg039CjxMtQ8howOS/bSAa8HiR/qjlyGF UzEw== X-Received: by 10.229.48.132 with SMTP id r4mr9777917qcf.5.1421982143172; Thu, 22 Jan 2015 19:02:23 -0800 (PST) MIME-Version: 1.0 Sender: yohgaki@gmail.com Received: by 10.229.93.70 with HTTP; Thu, 22 Jan 2015 19:01:42 -0800 (PST) In-Reply-To: References: Date: Fri, 23 Jan 2015 12:01:42 +0900 X-Google-Sender-Auth: _EWVtPm9KWOfPWgiDRN4pUoHzV0 Message-ID: To: Niklas Keller Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=001a1134050c8558c4050d49044e Subject: Re: [PHP-DEV] in Operator From: yohgaki@ohgaki.net (Yasuo Ohgaki) --001a1134050c8558c4050d49044e Content-Type: text/plain; charset=UTF-8 Hi Niklas, On Mon, Jan 19, 2015 at 9:42 AM, Niklas Keller wrote: > I'd like to propose a new feature to PHP: The in Operator > Bob mentioned a few weeks ago he wants such an operator in PHP and today I > stumbled over > > http://nikic.github.io/2012/07/27/How-to-add-new-syntactic-features-to-PHP.html > again, > which uses the in operator as a sample. > > Use cases: > > $obj = new StdClass; > $obj->prop = "value"; > $languages = ["PHP", "C", "Java"]; > var_dump("PHP" in $languages); // true > var_dump("Python" in $languages); // false > var_dump(0 in $languages); // false > var_dump("0x0" in ["0e3"]); // false > var_dump("prop" in $obj); // true > var_dump("foo" in $obj); // false > var_dump("Hello" in "Hello World!"); // true > > For strings, it would replace `strpos("Hello World!", "Hello") !== false)`, > for arrays it would replace `in_array($needle, $haystack, true)` > and for objects it would be a replacement of property_exists($class, > $property)`. > > This change would mainly be syntax sugar, but I think it's worth because: > * `"0x0" in ["0e3"]` would return false while `in_array("0x0", ["0e3"])` > returns true, that's probably an edge case, but there may be a lot of > people that don't use the `$strict` parameter, see http://3v4l.org/0K7E5 > * It would solve the issue that it's hard to remember the order of the > arguments for in_array and strpos, just had to look it up again. > > Bob would volunteer to implement that feature. > I would like to reserve 'in' and 'out' for Design by Contract (DbC). http://en.wikipedia.org/wiki/Design_by_contract D language has in and out like function foo() { in { // precondition } out { // postcondition } { // function body } Contract programing can be done only with assert or annotation. However, it's much easier/readable with native language support. Contract programming is proved to be fast/robust/secure. in operator can be useful, but it's the same as in_array(). If we need strict type check, we may do $ php -r 'var_dump(in_array("0x0", ["0e3"], TRUE));' bool(false) as you mentioned. I would like to reserve 'in' for DbC rather than syntax sugar. Regards, -- Yasuo Ohgaki yohgaki@ohgaki.net --001a1134050c8558c4050d49044e--