Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:72256 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 83460 invoked from network); 5 Feb 2014 10:08:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Feb 2014 10:08:16 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.169 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 209.85.212.169 mail-wi0-f169.google.com Received: from [209.85.212.169] ([209.85.212.169:48724] helo=mail-wi0-f169.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5C/61-09402-09D02F25 for ; Wed, 05 Feb 2014 05:08:16 -0500 Received: by mail-wi0-f169.google.com with SMTP id e4so376771wiv.2 for ; Wed, 05 Feb 2014 02:08:13 -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=GNon6Ie6PsRABcSwJ7F4wtuao0dXASiia39iFivanlk=; b=X7taqqKod5RNtH2XtN1K5LzjBB5F4NiTmuzotKv/e6gskvv76CJ6qtN4z019yZYKk/ PISILb0sDdNNEE4/D8c8jmlILRX6UQ8uC5GWi4yetVF9JerYc2hZ8Gl7Sor0IZIR+Gn+ O9Je5oe+px9kERUdYwnwwv5h98xrOuMT4nLOe97cHw5y/yZuLHFzPGO6rs2rwj0XBt3O Sr8bTUiQMKeUQfz97OUQvIHH9hTOZr14Dxufes0dSIA3ALMS5kWgxEhQoXPyxxH2VMBY 7yjjpw/8hCus5ZtKt7fN2qkP6PpT1Zz1Z/ITMJ+Bbr+WZATtecZmvp4AaHzlI8T/vrbO xn6g== X-Received: by 10.194.219.232 with SMTP id pr8mr647012wjc.6.1391594893782; Wed, 05 Feb 2014 02:08:13 -0800 (PST) Received: from [192.168.0.138] ([62.189.198.114]) by mx.google.com with ESMTPSA id uq2sm60095424wjc.5.2014.02.05.02.08.12 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 05 Feb 2014 02:08:13 -0800 (PST) Message-ID: <52F20D8B.9000003@gmail.com> Date: Wed, 05 Feb 2014 10:08:11 +0000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: internals@lists.php.net References: <52EE2B66.4040005@pthreads.org> <52F157BE.3020804@ajf.me> <52F15FD6.7060901@ajf.me> <52F1E58F.3050105@sugarcrm.com> <52F1EF12.2050705@sugarcrm.com> <52F1F675.2000600@sugarcrm.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] RE: RFC: expectations/assertions From: rowan.collins@gmail.com (Rowan Collins) Yasuo Ohgaki wrote (on 05/02/2014): > JavaScript implementations have different APIs. Node.js has closer API. > > [...] > > assert( > function() { > // Some check here > return FALSE; > }, > 'Error' > ); > > I would use at least, since I don't want to assert() related code to use > variable scope nor namespace. Actually, if you think this through with a real example, this only makes sense because of JS's scoping rules, which are very different from PHP's. A JS closure inherits the whole parent scope by default, and adds private vars to it, so this would be possible: function foo(arg) { assert ( function() { var assert_private_var = some_function(); return arg == assert_private_var; }, 'oops' ); } In PHP, there is no notion of inherited scope, so this would *not* work: function foo($arg) { assert ( function() { $assert_private_var = some_function(); return $arg == $assert_private_var; }, 'oops' ); } Since an assertion that can't actually check any state is useless for anything other than assert(false, 'Should never happen'), you'd always have to have a use() importing things: function foo($arg) { assert ( function() use ($arg) { $assert_private_var = some_function(); return $arg == $assert_private_var; }, 'oops' ); } That's a lot of boilerplate and room for mistakes, IMHO. Of course, if we could fix the parser to evaluate functions as soon as they're defined like in JS, you could make the whole thing into an expression which would work with the current (and proposed) assert: function foo($arg) { assert ( (function() use ($arg) { $assert_private_var = some_function(); return $arg == $assert_private_var; })(), 'oops' ); } Not pretty, but it would give you your local scope. Regards, -- Rowan Collins [IMSoP]