[PEAK] Generic Function API enhancement ideas
Phillip J. Eby
pje at telecommunity.com
Fri Dec 31 11:01:29 EST 2004
At 10:48 AM 12/31/04 -0500, Lloyd Kvam wrote:
>Since these are simply strings, wouldn't it be reasonable to expect
>people to use Python's string interpolation to handle duplication rather
>than creating something else for people to learn? The template facility
>in 2.4
>http://python.org/doc/2.4/whatsnew/node5.html
>provides another string substitution approach.
There are a couple of issues with that; first, operator precedence and
parenthesizing. That is, you'll need to be careful to put parentheses
around things to ensure that parsing occurs correctly in all cases. If you
forget, things will sometimes break. The macro idea I have will not have
this problem, because the substitution is in the AST, not in the string.
Second, the point of having the macros in the generic() or when() calls, is
so that you can reuse the expressions in the function body without
recomputing them. String substitution doesn't offer any way to accomplish
that. Last, but not least, string substitution is considerably more
verbose, and the old format (which we'd have to use, unless we backport the
'string' module) is error prone to use as well.
While I was playing around with the bowling example, at one point I
actually tried string concatenation to abstract some of the predicates, but
it looked awful, and it was very awkward to do the refactoring.
More information about the PEAK
mailing list