Home   Archive   Permalink



Be careful when using FUNCT

Hi, you know FUNCT was added to R2.7.7. It defines a function with all set-words as locals. Mostly for us, lazy programmers may be :)
    
I faced a problem when using this function, I had a reasonably big function in a reasonably big object (gui). First I use that function just for its return value so everything was ok.
But then I added some code to change some gui vars. It didn't work with NO error ofcourse.
It took thirty minutes to find out what it going on, I PROBED everything, everything had a value, so why it didn't work. Then I remembered I used FUNCT.
    
Here is a quick example:
     b: 1
     o: context [
         a: 1
         f: funct [][a: 2 b: 2]
     ]
    
     o/f
     o/a
     == 1 ;not 2
    
Of course it is easy to see the problem when your object/function is not that big. But when they are getting bigger or if you added some new codes a few months later then you may forget.
    
I have a quick solution, I changed FUNCT color to red using my text editor's syntax highlighting feature :)


posted by:   Endo       2-Nov-2010/3:57:52-7:00



Ofcourse you can do this, it will work as expected:
     o: context [
         a: 1
         f: funct [][o/a: 2 b: 2]
     ]

posted by:   Endo       2-Nov-2010/7:13:10-7:00



Thank you for the heads up Endo :)

posted by:   Nick       7-Nov-2010/4:09:51-8:00



Try this:
     o: context [
         a: 1
         f: funct/with [][a: 2 b: 2] self
     ]
    
That binds the function to the object first and excludes the object's words from the function's locals. When FUNCT/with is passed an object reference the function becomes something like a friend function in C++. When passed a block the function gets a local object to store static words in.

posted by:   BrianH       10-Dec-2010/0:23:11-8:00



Thank you BrainH, that is great.
Here is an example usage:
    
f: funct/with [] [b: b + 1] [b: 1]


posted by:   Endo       10-Dec-2010/8:14:55-8:00