Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:82328 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 86615 invoked from network); 10 Feb 2015 04:43:32 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 10 Feb 2015 04:43:32 -0000 Authentication-Results: pb1.pair.com smtp.mail=dmitry@zend.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=dmitry@zend.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain zend.com designates 209.85.220.170 as permitted sender) X-PHP-List-Original-Sender: dmitry@zend.com X-Host-Fingerprint: 209.85.220.170 mail-vc0-f170.google.com Received: from [209.85.220.170] ([209.85.220.170:53470] helo=mail-vc0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5E/74-47508-37C89D45 for ; Mon, 09 Feb 2015 23:43:32 -0500 Received: by mail-vc0-f170.google.com with SMTP id hq12so2733984vcb.1 for ; Mon, 09 Feb 2015 20:43:28 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=MXcUYe4aGj4SX536tylPYbmdD7D7mJlozKfezkj9dUI=; b=EaMxh46iDzgIMC2Rcej6e1eqzqAueOho4NsW9fh3HcRHhMChBNeElBgdxOX0Elaf+S A0dgDChEpl2SWkCDs/h7e7luUIMZqLrFdGFF1l/VaKRGhBq+frbeTMjB5Wohi2UTvtdD sH73HNIYlioDvmCkN+/7dD9uypaFstDFzXsjLSNDh2oyQ543W8rBUlj8pgKF700amVsi XQTGVlMD61X1gvU3KgBHstn/f0r+J1dy1pCYGAdoJwSdmRNJ8vdcapYYSLPKWJRWSCIQ VYNQH8S6wLdgL3SUliu+ZymCY0D4zDPP8dCOggBvVK1pIAXsy4Dtj4aRj4rYgR/kopHC sMIA== X-Gm-Message-State: ALoCoQnXUyfPhNWWciC37l74CsHxZBh1YWNbTv6CJ6eZXvZ/vNzmykaG1PYR85Sf7pcRmLXenGoGiTGFAXKKnEE7Am8prCNFjQeO8FRfNMipahK2DUvnOcjWrL+PZyYT/Eh9w+XUHthvqn1QAGk1Wxp16KT978SrQg== MIME-Version: 1.0 X-Received: by 10.221.21.131 with SMTP id qs3mr13273326vcb.33.1423543408372; Mon, 09 Feb 2015 20:43:28 -0800 (PST) Received: by 10.52.74.73 with HTTP; Mon, 9 Feb 2015 20:43:27 -0800 (PST) Received: by 10.52.74.73 with HTTP; Mon, 9 Feb 2015 20:43:27 -0800 (PST) In-Reply-To: References: <54D7ED22.3080001@gmail.com> Date: Tue, 10 Feb 2015 08:43:27 +0400 Message-ID: To: Yasuo Ohgaki Cc: PHP Internals , Stanislav Malyshev , Joe Watkins Content-Type: multipart/alternative; boundary=001a11339f8a2dc4e8050eb48743 Subject: Re: [PHP-DEV] Design by Contract From: dmitry@zend.com (Dmitry Stogov) --001a11339f8a2dc4e8050eb48743 Content-Type: text/plain; charset=UTF-8 On Feb 9, 2015 11:20 PM, "Yasuo Ohgaki" wrote: > > Hi Dmitry and Joe, > > On Mon, Feb 9, 2015 at 8:27 PM, Dmitry Stogov wrote: >> >> yes. this may work. >> probably better to put it after extends and implements. >> >> >> Dmitry. >> >> On Mon, Feb 9, 2015 at 2:19 PM, Joe Watkins wrote: >>> >>> Could this be described as a requirement of the class ? >>> >>> class Mine >>> require(invariant-expr) >>> extends Something >>> implements Someface { >>> >>> public function method($param) : return >>> require(input-expr), >>> return(output-expr) { >>> >>> } >>> } >>> >>> To avoid invariant keyword maybe. > > > This would work. > If users have adopted DbC in some way, 'invariant' may be used already. > > I see two issues. > > Interface works, but most classes are class without interfaces. Then users have to repeat > require()/return() many times to check class state or have to use interface for DbC. > In D classes may have "invariant" constraints. We may use "require" keyword for it. The constraints ineritance rules and order of calls to constrains must be the same s in D. class Foo { private $sum = 0; require($this->sum >= 0); // invariant constraint will be called before and after every method function add($n) { $this->sum += $n; } } > Since compiler does not know a method() is for DbC invariant, it will be compiled and exists > in production execution. > > Use of interface: > - no additional keyword (pros) > - requires interface for DbC, most classes does not require interface (cons) > - if interface is not used, user has to repeat invariant conditions over and over (cons) > - requires to define method that should not exist in production (cons) I didn't understand you idea. > > New keyword: > - does not require interface for efficient definition (pros). > - new keyword (cons) > > It seems we are better to choose proper keyword for 'invariant'. 'invariant' is not common, so 'invariant' > may be good enough choice. Does anyone use 'invariant' as function/method/class/constant names? > If there is better name, suggestion is appreciated. > > On place closure call like javascript is not supported in PHP, but function works with assert. > > function foo() { return FALSE; } > assert(foo()); > ?> > PHP Warning: assert(): Assertion failed in - on line 3 > > This wouldn't be changed for require()/return()/invariant()? > > We need a switch to change development/production. I'll use "dbc=On/Off" for now. > If you have any better idea, please let me know. (dbc will be INI_SYSTEM) Check the "expectations" RFC. I think, it's going to be 3 state switch, zero-cost disable, run-time disable, run-time enable. So, it may be INI_ALL, but it won't be possible to switch from/to zero-cost at run-time. > > For CLI, there will be no command line switch for dbc. It executes script production mode by > default. If user needs development mode > > php -d dbc=1 script.php > > should be used. > > > And finally, are we going to allow custom assertion error message? e.g. > > require($a > 0, 'Parameter $a must be positive number'); I think, it may be useful. > > Since this contract is definition like "implements"/"extends", we may not allow > custom error message. I'll write the RFC not to allow custom error messages unless > you dislike it. > > I think we have enough consensus/information to start writing the RFC. > If I have any concern, I'll ask here. Ok, go forward :) Thanks. Dmitry. > > Regards, > > P.S. If anyone finds issues with DbC as class definition, please let me know. > > -- > Yasuo Ohgaki > yohgaki@ohgaki.net > --001a11339f8a2dc4e8050eb48743--