Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81046 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 33411 invoked from network); 23 Jan 2015 14:58:32 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Jan 2015 14:58:32 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.175 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.175 mail-we0-f175.google.com Received: from [74.125.82.175] ([74.125.82.175:48669] helo=mail-we0-f175.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 53/58-25368-79162C45 for ; Fri, 23 Jan 2015 09:58:32 -0500 Received: by mail-we0-f175.google.com with SMTP id p10so4494283wes.6 for ; Fri, 23 Jan 2015 06:58:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=k8K3Hx3eNv0OTB2zylrD3I/uhfr3gbW3KQvDaZ1C2qE=; b=esXub7Sk81AiGNFv9c5M4SWkmRwqxOKpzO+zlcPOxEjQdUKdrwrMSOLKl5bOO5ri1t 9ZHFAvynX19z8beh9Hy27Vo6t3uyzJwCjBLUv7dgO6hBFL/9UQTGxwjFSO0mE94C0VdR DdVDImAavXriJcuB014GWYWIsMAfDNA275PkQjwVfwksRkbCiGNKHbt3lT+GGG6aS7lT 1BEmoC67eN0eRPvOrwB/bX0czHQU2kBQ/AWNSSB+huUqSbYlFzqTNbVhVrmCv0lELsxC wOnZC7u9BvxQcP833jINNf7CtKyY1Jp+7S3mjg/61NgG0da1i6D387eHbnSiETCJBQpW 2IWQ== X-Received: by 10.195.13.77 with SMTP id ew13mr14535058wjd.106.1422025108903; Fri, 23 Jan 2015 06:58:28 -0800 (PST) Received: from [192.168.0.172] ([62.189.198.114]) by mx.google.com with ESMTPSA id dz10sm2159300wib.17.2015.01.23.06.58.27 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 23 Jan 2015 06:58:28 -0800 (PST) Message-ID: <54C26153.9090701@gmail.com> Date: Fri, 23 Jan 2015 14:57:23 +0000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] in Operator From: rowan.collins@gmail.com (Rowan Collins) Yasuo Ohgaki wrote on 23/01/2015 03:01: > 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, seehttp://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 > } Would this conflict? It seems to me that this syntax could only ever exist in a declaration (function signature) context, and the proposed in operator could only ever exist in a procedural code (function body) context, which are surely completely separate sections of the parser. If anything, wanting the keyword for another purpose might strengthen its case, because each feature isn't reserving a new word. It's not the first keyword to have distinct meanings by context - "use" means very different things for namespaces, traits, and closures. Regards, -- Rowan Collins [IMSoP]