3 ms·
The idea of routing annotations is used extensively in the Recess PHP Framework, except that in Recess the annotation also takes the HTTP verb (GET, PUT, POST,
by KrisJordan 17y ago
The idea of routing annotations is used extensively in the Recess PHP Framework, except that in Recess the annotation also takes the HTTP verb (GET, PUT, POST, etc.) to make beautiful, RESTful URLs even easier to work with.
http://www.recessframework.org/page/routing-in-recess-screencast http://www.recessframework.org/page/routing-in-recess-screen...
- jrockway 17y agoYou know you have a good programming language when comments are a key part of the control flow.
- jawngee 17y agoReally? That's your contribution to the discussion? I have a lot of negative things to say about a lot of different languages, but I think what Kris has done with annotations in PHP is pretty cool, even if it is ultimately a hack. PS. Juno looks nice though I'm not a huge fan of frameworks that return rendered html from controllers. But I do like it's Sinatra-esque simplicity.
- hesdeadjim 17y agoWhile I respect the cleverness of his hack, it's the kind of thing that sets off alarm bells in my head. Much like fancy template metaprogramming tricks in C++ did, only worse since it actually breaks the semantic contract that the language imposes for comment blocks.
- jawngee 17y agoYou're way over thinking the problem. I'm not so sure there is a "semantic contract" for comment blocks since they're made available via reflection. Comments are metadata after all. And this is more akin to annotations/attributes ala python, java, C#. C++ doesn't have any feature that is approximate by virtue that annotations are aspects of reflection and C++ doesn't have reflection.
- cnlwsu 17y agoI dont know if I would call a comment(part of language that has meaning only to the programmers) equivalent to a language defined thing like annotations/attributes which has specific meaning to the compiler/interpreter. But I do aggree it CAN be metadata in the same regard a file name or path can be (which many frameworks use).
- jrockway 17y agoUsing docstrings as executable code is an indication that your language lacks actual extensibility.
- randallsquared 17y agoHowever, for PHP, we really didn't need the indication. :) I use PHP every day, though, and it gets the job done.
- KrisJordan 17y agoThey're not used as executable code. They're used as meta data. Not much different than an XML file sitting beside of a script, except that 1) it's more DRY because for external files to say something about a construct in code there is repeated information, i.e. <route><to><class>Foo</class><method>bar</method></to><from><method>GET</method><url>some/url/here</url></to></route>, 2) because you're using the PHP language to specify metadata about a PHP construct refactoring is easier, i.e. change the name of a function and you don't have to go update your routing somewhere else, 3) having the metadata inline with code turns out to be mentally lightweight once you get over the initial shock of seeing metadata in a comment.
- KrisJordan 17y agoPHP has its flaws, no doubt. It's little known that doccomments are a PHP language construct that can be reflected over just as Python's doc strings can be introspected with __doc__. http://us3.php.net/manual/en/language.oop5.reflection.php#language.oop5.reflection.reflectionclass http://us3.php.net/manual/en/language.oop5.reflection.php#la... Thus !Route GET, a/good/$programmer is just as much a language construct in a PHP program as @route('a/good/:programmer') is in a Python program. The key difference is Python defines at the language level how that particular bit of script is expanded as a decorator. Recess does its expansion of annotations at the framework level. Python is a much better and more thoughtfully designed programming language than PHP, for sure. PHP has loop holes, doccomments is one of them. PHP does not do anything, by default, with a doccomment, but it does give an opportunity to introduce meta-data on classes, methods, and properties and easily reflect over that meta-data and then it's up to the programmer to define a 'DSL'. In this sense PHP offers something powerful and dangerous. Recess choose a simple data format, such that parsing can be expressed in a regexp that is then processed by essentially 'Constructing a class of type [FirstWord] and call init method with array of [Following words as a PHP array] - finally make these annotations available through reflection'. Not too much different than annotations in Java/C#. Simple enough that the danger is mostly negated and the utility makes the 'hack' justifiable. For a better-written justification of why this design decision was made for Recess: http://www.recessframework.org/page/on-hiding-our-dick-tracy-underwear-under-annotations http://www.recessframework.org/page/on-hiding-our-dick-tracy... (Aside: This is why I'm a huge fan of Scala - extending a language with the language is awesome.)
- intranation 17y agoJust an aside, @someting(blah) isn't a comment or docstring--it's a decorator. So apples to oranges and all that. The equivalent in Python would be: def my_controller(request): """route: /thing/:var/""" pass or similar.