4 Replies Latest reply: Mar 3, 2008 9:32 AM by sneakyimp RSS

    checking function variable

    sneakyimp Community Member
      I have some code that tries to assign a static class function to a function variable. After assigning it, I'd like to check and make sure that a function has been assigned. The code attached is causing an error "Warning: 3553: Function value used where type Object was expected. Possibly the parentheses () are missing after this function reference."

      Is there some better way to check and see if a valid function has been assigned to my vars serialize and unserialize ? They are declared like this:

      private var serialize:Function;
      private var unserialize:Function;
        • 1. Re: checking function variable
          Raymond Basque Community Member
          Test against null.

          if (serialize==null || unserialize==null){}
          • 2. Re: checking function variable
            sneakyimp Community Member
            Thanks for the response.

            I fail to see why this usage would not invoke the function just like my original approach but apparently it does not. This seems really arbitrary to me. Is there any reason why this works and the other way does not?
            • 3. Re: checking function variable
              Newsgroup_User Community Member
              The functions are not being invoked -- it's the compiler doing type checking
              and warning you of possible errors. The compiler sees functions in the
              expression and is assuming you want return values from those functions.

              You can use untyped references if you want to avoid the compiler warnings.
              i.e.

              private var serialize:*;
              private var unserialize:*;

              if (!serialize || !unserialize)
              {
              }





              • 4. Re: checking function variable
                sneakyimp Community Member
                It occurred to me that what I really want to do is make sure this var is a function. After all, a scalar value like 3 that is not null would pass the test but wouldn't satisfy my need for a valid function.

                I noticed in someone else's code the use of 'is' and a type. This code appears to be helpful for this:

                var myFunc:Function;
                if (myFunc is Function) {
                trace('IS FUNCTION');
                } else {
                trace('IS NOT FUNCTION');
                }
                myFunc = Math.sin;
                trace('assigned');
                if (myFunc is Function) {
                trace('IS FUNCTION');
                } else {
                trace('IS NOT FUNCTION');
                }