A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2002-March/021415.html below:

[Python-Dev] A Hygienic Macro System in Python?

[Python-Dev] A Hygienic Macro System in Python?Daniel Mahler mahler@cyc.com
Mon, 18 Mar 2002 19:54:28 -0600
I this 
Skip Montanaro writes:

 >     Tim> OTOH, there are cases like
 > 
 >     Tim>     lock.acquire()
 >     Tim>     try:
 >     Tim>         do stuff
 >     Tim>     finally:
 >     Tim>         lock.release()
 > 
      ===>
 >     Tim>     withlock lock:
 >     Tim>         do stuff
 > 
 >       ...
 > Using the same argument as "if Guido wanted 'unless' he'd add it
 > to the language", I think that if this is important enough, it's important
 > enough to add to the language.  I think a "lock" keyword would be
 > appropriate:
 > 
 >     lock somelock:
 >         do stuff
 > 

I think this misses a more general point behind Tims post.
There are many APIs that lead you to write code with certain fixed
parts. Since Python like most languges has eager evaluation,
procedures cannot be used to abstract out the fixed parts
(usually initialization and clean up, but not always)

eg:

 #imagine this is some connection protocol for some kind of server

  withConnectionTo connVar address:
     <statements>

 ==>
  
  connVar = Connect(address)
  try:
    <statements>
  finally:
    connVar.disconnect()

[ I presume nobody really wants people to implement

  def withConnectionTo(connVar,address,statements):
    exec("""

  %s = Connect(%s)
  try:
    %s
  finally:
    %s.disconnect()

         """ % (connVar,address,statements, connVar)
         )
]


There is a potential infinity of code templates like this that can get annoying,
and which cannot be abstracted out procedurally in a natural way.



I think the thing worth noting here is that the introduced constructs
would usually be colon&indent type entities (what do we call those?)
One application of definable syntax is compositional *incremental*
output without the need to build up a string
or some other kind of datastructure to represent the entire document first
which can get horribly expensive for complex documents.
Consider a html printer macro library:

html:
    header:
        title:
            print "blah"
    body:
        h1:
            print "bleh"
        p:
            print "foo" 
            a (href="http://bar.org"):
                print "bar"
            print "baz"

Daniel Mahler





RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4