Re[2]: Function once: concern

Lenard Lindstrom <[email protected]>
Newsgroups gmane.comp.lang.prothon.user
Message-ID <Mahogany-0.66.0-4294849213-20040713-151043.00@pop3.norton.antivirus>
On Tue, 13 Jul 2004 13:57:07 -0700 Mark Hahn <[email protected]> wrote:

> Lenard Lindstrom wrote:
> 
> > A while back it was decided to use the anon, by convension, 
> > for declaring anonymous function. As such it is just a 
> > throw-away name. But this causes a problem for anonymous 
> > functions with a once: section. Take this example:
> > 
> > handlers = {}
> > 
> > def anon():  # Version 1
> >     once:
> >         anon.const = 1
> >     print(anon.const)
> > handlers[1] = anon
> > 
> > def anon():  # Version 2
> >     once:
> >         anon.const = 2
> >     print(anon.const)
> > handlers[2] = anon
> > 
> > handlers[1]()  # Prints "1"
> > handlers[2]()  # Prints "2"
> > handlers[1]()  # Intend to print "1"; will likely print "2"
> > 
> > The second anon declaration replaces the Version 1 anon 
> > function object with a Version 2 object. This is what a 
> > Version 1 call will access when called the second time.
> > 
> > I think the following will solve the problem. When a function 
> > is defined its instance is automatically assigned to a 
> > function local variable of the same name.
> > 
> > def somename():
> >     once:
> >         somename.attr = somevalue  # "somename" is local to 
> > function, like a closure.
> >     do_something(somename)         # also local "somename" access.
> > 
> > # Though bad form, changing the name of function "somename" 
> > to "foo" and # identifier "somename" to something else at the 
> > module level does not break # the function now known as 
> > "foo". Within its code block "foo" will always # be 
> > "somename". foo = somename somename = something_else
> > 
> > This is not be a problem with the init: section since the 
> > outer function identifier is referencing the proper function 
> > object during the function declaration.
> > 
> > P.S. This mechanism could also be applied to the object 
> > statement so that reusing a prototype name at the module 
> > level, though again not good form, will not break previous 
> > prototypes created under that name.
> 
> Doesn't this have the danger of confusing the user by doing a hidden
> shadowing?  He may code something expecting to see the outer.func
> change.
> 
Those wrapper things again. Well, there is this:

def foo():
    init:
        object scope:
            me = foo
            prevScope_ = foo.prevScope_
        foo.prevScope_ = scope
    once:
        me.x = 12
    print(me.x)

foo()   # print 12
foo.x   # 12

:-)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.