On Mon, Feb 16, 2015 at 10:15 PM, Dmitry Stogov <dmi...@zend.com> wrote:
> Hi Nikita, > > On Tue, Feb 17, 2015 at 12:07 AM, Nikita Popov <nikita....@gmail.com> > wrote: > >> On Mon, Feb 16, 2015 at 10:02 PM, Dmitry Stogov <dmi...@zend.com> wrote: >> >>> >>> >>> On Mon, Feb 16, 2015 at 11:05 PM, Benjamin Eberlei <kont...@beberlei.de> >>> wrote: >>> >>>> >>>> >>>> On Mon, Feb 16, 2015 at 12:07 PM, Dmitry Stogov <dmi...@zend.com> >>>> wrote: >>>> >>>>> hi, >>>>> >>>>> During discussion of different ways of implementing "Design by >>>>> Contract" we >>>>> got an idea of using annotations. >>>>> >>>>> BTW: annotations are useful by their own and may be used for different >>>>> purposes. Support for annotations was proposed long time ago: >>>>> >>>>> https://wiki.php.net/rfc/annotations >>>>> https://wiki.php.net/rfc/annotations-in-docblock >>>>> https://wiki.php.net/rfc/reflection_doccomment_annotations >>>>> >>>>> HHVM already implemented similar concept >>>>> >>>>> http://docs.hhvm.com/manual/en/hack.attributes.php >>>>> >>>>> I made a quick and dirty PoC that shows how we may implement >>>>> annotations in >>>>> PHP7 and how powerful they may be : >>>>> https://gist.github.com/dstogov/dbf2a8f46e43719bd2c2 >>>> >>>> >>>> How about not exposing the AST, and then the userland can pass context >>>> to a method $reflection->getAnnotation($name, $context); >>>> >>>> https://gist.github.com/beberlei/0dc36d4412b8e3454912 >>>> >>>> That way AST is not exposed to userland. >>>> >>> >>> Your example handles just particular use-case. >>> I would like to provide a general solution. >>> >>> Thanks. Dmitry. >>> >> >> Could you provide some more info on how you would imagine this to be used >> for the DbC use case? As far as I see, this would require injecting code >> into the method body based on the annotations. Does that mean that >> annotations handlers will get the chance to modify the AST of the method >> itself during compilation? Or how is this supposed to be realized? >> > > In general, it must be possible to plug into compilation process using > zend_ast_process() callback and modify function AST, inserting constraints > validation in proper places. > But for now, I'm thinking only about metadata syntax and representation. I > just showed, how it can be used to represent DbC constraints. > I don't think we need user-level interface to modify AST. > To make sure I got it right: The AST-based annotations are only there to allow handling by PHP extensions (or the engine itself), while evaluated annotations are there for use by userland code? Nikita