2 Replies Latest reply on Nov 30, 2009 2:25 PM by msakrejda

    Functions: how first-class are they?

    msakrejda Level 4

      One of my favorite things about Flex is first-class functions. They make my life easy. I recently wanted to add some utility functions to my app, and since Flex has first-class functions, I thought to myself, I can just define them outside of a class, instead of having to define (and use, more importantly) static methods on a class. Apparently, FlexBuilder does not like this--it does not complain about the definition, but when I try to use it, it does not recognize that the symbol is defined. Is there a way to define publically visible first-class functions (rather than static methods), like escape, isNaN, trace, etc? Ideally, I'd like these in a package or namespace, but I'm curious in any case.

        • 1. Re: Functions: how first-class are they?
          paul.williams Level 4

          I believe you can, but the rules for public functions are similar to those for classes:

           

          Only one public function per AS file

          Filename must match public function name

          Public function must be placed within a package declaration

          Private functions must be placed outside and after package declaration

          You must import the function before you use it

           

          Eg:

           

          package funcs
          {
              public function publicFunc() : int
              {
                  return 4 + privateFuncA() * privateFuncB();
              }
          }

           

          function privateFuncA() : int
          {
              return 4;
          }

           

          function privateFuncB() : int
          {
              return 2;
          }

           

          You would use the above by importing publicFunc and then calling it:

           

          import funcs.publicFunc;

           

          ...

           

          var result : int = publicFunc();

          • 2. Re: Functions: how first-class are they?
            msakrejda Level 4

            On Mon, Nov 30, 2009 at 2:14 PM, paul.williams <forums@adobe.com> wrote:


            > I believe you can, but the rules for public functions are similar to those for classes:

            > Only one public function per AS file
            > Filename must match public function name

             

            These are what I was missing. It's working now--thank you.