Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:82212 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 18459 invoked from network); 9 Feb 2015 02:58:38 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 9 Feb 2015 02:58:38 -0000 Authentication-Results: pb1.pair.com header.from=guilhermeblanco@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=guilhermeblanco@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.223.169 as permitted sender) X-PHP-List-Original-Sender: guilhermeblanco@gmail.com X-Host-Fingerprint: 209.85.223.169 mail-ie0-f169.google.com Received: from [209.85.223.169] ([209.85.223.169:37601] helo=mail-ie0-f169.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 48/02-26926-C5228D45 for ; Sun, 08 Feb 2015 21:58:37 -0500 Received: by iebtr6 with SMTP id tr6so13470498ieb.4 for ; Sun, 08 Feb 2015 18:58:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=CahutRUIWHihhEt26B5Kotk464+qlgyzIeQtB+YQoBo=; b=dR35BAazyhbIPluQTeYIGa2HeIEvjHdYTpbvfu+ZegXHv2cqDAOQN/ZuP+R/rEATQe yphs6vhiRc/TYwyRUAPsyBpXuIYIjqZO3pBIW7ECK2DamaVOrtEASl/JwXXRn2DfChqP WbhIs7yNDUIbbE7Dl15d37f++M+SPrkI0v81gbRKJb11LBf5p17FcOuzdGGK05nK8Liq VtfA3Cf7T8WXNx8ybepRaD6M7jKj9o+f4sJG2LcZq+Qz14dHxec4Hf7K1Rv5wdJnjfzw g+gSVOqsOYmXRahqFdKPs26PQfCIiOhuh6fMcWdXNqf2+YTvRe5vGO6Gz50c5mtWlm4x Tevg== X-Received: by 10.107.39.67 with SMTP id n64mr23192822ion.36.1423450714182; Sun, 08 Feb 2015 18:58:34 -0800 (PST) MIME-Version: 1.0 Received: by 10.64.238.75 with HTTP; Sun, 8 Feb 2015 18:58:13 -0800 (PST) In-Reply-To: References: <54D7ED22.3080001@gmail.com> Date: Sun, 8 Feb 2015 21:58:13 -0500 Message-ID: To: Yasuo Ohgaki Cc: Stanislav Malyshev , Dmitry Stogov , PHP Internals Content-Type: multipart/alternative; boundary=001a11407bc42c9b59050e9ef2f2 Subject: Re: [PHP-DEV] Design by Contract From: guilhermeblanco@gmail.com ("guilhermeblanco@gmail.com") --001a11407bc42c9b59050e9ef2f2 Content-Type: text/plain; charset=UTF-8 Hi Yasuo, Design by Contract could be used at the top of Annotation if (and only if) it also had support for Interceptors. Since it could potentially be a nightmare for PHP, I don't think it's time to propose something at the top of another thing that is still far from being reality. It would follow somehow a similar approach of what Symfony validators do: http://symfony.com/doc/current/book/validation.html but it would be called at possible 3 times: pre-execution, in-execution (using around and bound to an external class) and post-execution. However, I keep myself asking if this is a good idea. It obviously brings a certain level of AOP to PHP, but I'd rather take a simplistic approach such as the one suggested by Stas but with a few adjustments. His approach is more inline to C and C++, but I like a more customizable Java style assertion support http://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html Example: function foo($name) { assert $name !== '': "Name must not be an empty value"; } This would be similar to this: function foo($name) { if ($name === null) { throw new AssertionError("Name must not be an empty value"); } } Basically, new grammar to be supported would be something like this: assert_statement: T_ASSERT expr ':' expr ';' ; Where the first expr is a boolean expression and the second expr must return a value (cannot be null). This would be a good start for PHP, even though Java does not recommend to use this for argument's method check ( http://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html#usage), but I consider that since PHP is not a strict language, we do not have the same privileges by automating this check form arguments itself. That is my personal suggestion for DbC, which does not fully align with what it was proposed, but since you asked... here it is. []s, On Sun, Feb 8, 2015 at 8:22 PM, Yasuo Ohgaki wrote: > Hi Stas, > > On Mon, Feb 9, 2015 at 8:11 AM, Stanislav Malyshev > wrote: > > > > Following our conversation, I tried to imagine how DbC should look like > > in > > > PHP from user perspective. Finally, I was influenced by the semantic > > > proposed in D, and syntax proposed for Java. So, these are my initial > > > thoughts: > > > > > > For php it may look like the following: > > > > > > function foo() > > > requre() > > > ensure() > > > { > > > ... > > > } > > > > Why not do it simpler? > > > > function foo() { > > // require > > assert(); > > ... > > // ensure > > assert(); > > } > > > > I'm oversimplifying a bit, but in general, why we need more places to > > have code in the function than the actual code of the function? It would > > be harder to parse, to read, to maintain, to debug, to profile, etc. and > > I'm not sure what exactly it provides that can't be done by plain > > regular code inside the function. > > > > If we're concerned about the costs of assert, we could make special > > provision in the compiler for zero-cost asserts. It doesn't require > > moving code out of the function. > > > Interesting idea. I like it. > The only draw back I can see now is that this may make post/pre condition > retrieving > harder for automatic documentation. We may think of solution for this. > > Regards, > > -- > Yasuo Ohgaki > yohgaki@ohgaki.net > -- Guilherme Blanco MSN: guilhermeblanco@hotmail.com GTalk: guilhermeblanco Toronto - ON/Canada --001a11407bc42c9b59050e9ef2f2--