5 Replies Latest reply on Oct 17, 2010 10:42 PM by areohbee

    Use of "strict"?

    johnrellis Most Valuable Participant

      I just about the standard Lua script:

       

      require 'strict'

       

      that will raise an error if you try to access an "undeclared" global variable (where "declared" means it was assigned at the top level of a script).  I wish I had found this two months ago (it's described in "Programming in Lua, 2nd Ed.", but I must have missed it first time through).   Almost all of my "undeclared" globals are either typos or missing "local" declarations.

       

      Does anyone use this with their plugin development?  Is there a reason not to use it?

        • 1. Re: Use of "strict"?
          areohbee Level 5

          johnrellis wrote:

           

          Is there a reason not to use it?

           

          Does it work?

           

           

          I think Lr2 is "strict" by default, Lr3 is not. - I've had plugins that work in Lr3 crash in Lr2 due to accessing unitinialized global variables.

           

          I would definitely prefer 'strict' for any Lr3 plugins that need to maintain compatibility with Lr2 as well...

           

          In fact, although I've taken advantage of "non-strictness" in Lr3, I don't think I would consider it a "best practice" in most situations - kinda like using the same variable for a table, then an unrelated string, then a random number... - you can, but that doesn't mean you should...

           

          While I'm at it, declaring an SDK version 2.0 does not necessarily mean you'll get an error in Lr3 when doing things in Lr3 that are not supported in Lr2 - this is an example.

           

          Rob

          • 2. Re: Use of "strict"?
            johnrellis Most Valuable Participant

            So far, it seems to work very well.  I just copied "strict.lua" from the Lua distribution and do

             

            require 'strict'

             

            and it catches most of my mistakes.  (It doesn't catch the times I make a function global rather than local.)

            • 3. Re: Use of "strict"?
              areohbee Level 5

              Excellent.

               

              Maybe a variation of dump(object), say dumpFunctions(object), then

               

              dumpFunctions( _G ) to catch your global functions.

               

              ?

               

              R

              • 4. Re: Use of "strict"?
                johnrellis Most Valuable Participant

                Yeah, I started doing that, but now I've gone off the deep end and written a mini-debugger that does all sorts of stuff as well as your suggestion.  After I use it for a few for days, I'll make it available for others to try.  It's limited in key ways due to Lightroom crippling the "debug." functions, but I am still able to have it do a fair amount (breakpoints on functions, stack trace, interactive read-eval-print). 

                • 5. Re: Use of "strict"?
                  areohbee Level 5

                  John - You never cease to amaze me!

                   

                  _R