Re: Design by Contract
| From: | Dmitry Stogov | Date: | Wed, 11 Feb 2015 11:25:35 +0000 |
| Subject: | Re: Design by Contract | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-82470@lists.php.net to get a copy of this message | ||
On Wed, Feb 11, 2015 at 10:58 AM, Stanislav Malyshev <smalyshev@gmail.com>
wrote:
> Hi!
>
> > 1) contracts in doc-commetns https://wiki.php.net/rfc/dbc
> >
> > /**
> > * @requires ($a >= 0)
> > */
> > function foo($a) {
> > }
>
> We've spent several years rejecting annotations because no information
> can be in comments, not even a little bit, not even a tiny. Now we'd
> have full-blown running code in comments? How would this code be
> debugged/profiled/interpreted by tools? Writing PHP parser is not easy
> right now, but this would turn it into nightmare (what if you have a
> comment inside @requires? etc.)
>
I don't see how this can be implemented in PHP core as well.
This approach is for external tools.
>
> > 2) contracts as part of the language
> > https://wiki.php.net/rfc/dbc2
> >
> > function foo($a)
> > require($a >= 0)
> > {
> > }
>
> This is a step better but still we have the similar issues with
> readability, to which reuse of 'require' is added.
>
> > 3) contracts in annotations
> >
> > <<requires($a >= 0)>>
> > function foo($a) {
> > }
>
> That one actually looks better to me, but: I'm not sure how annotation
> syntax is supposed to support expressions or closures,
keep AST.
> yet more - how it
> is supposed to share the scope with the function. I.e., if you define
> annotation class/method/function/data structure/whatever it is, what it
> is supposed to receive?
We may have AST transformation pass that would convert these annotations
into assert() and insert them into AST of the function.
> Is it some special form of annotation for this
> purpose only (meh)?
>
yes. some special attributes. requires/ensures/invariant
> Oh, and <<>> syntax is *ugly* ;)
>
It's from HHVM. I don't like it as well, please, propose the better one.
Thanks. Dmitry.
>
> --
> Stas Malyshev
> smalyshev@gmail.com
>