7 Replies Latest reply on Nov 18, 2008 4:43 PM by *gsb*

    Altering Object class source

    Slash_Wannabe

      For whatever reason the ActionScript base class Object has no equals method. I want to change this so extending classes can override this method to define their own equality. Extending Object to include an equals method gets a bit messy since I would need to support the "primitive" classes, like String, which extend Object.

      I've added an implementation to Object in Global.as and this goes as far as tricking the compiler. However during execution a method not found exception is thrown. Looking at Global.as it seems that this file doesn't contain implementations of methods.

      Anyone have an idea of where the implementation for the library classes are? Perhaps in the player itself? Additionally, am I running the risk of player version incompatibilities?
        • 1. Re: Altering Object class source
          Level 7
          I think there's an ObjectUtils class.

          HTH;

          Amy

          "Slash Wannabe" <webforumsuser@macromedia.com> wrote in message
          news:gfs91u$3iq$1@forums.macromedia.com...
          > For whatever reason the ActionScript base class Object has no equals
          > method. I
          > want to change this so extending classes can override this method to
          > define
          > their own equality. Extending Object to include an equals method gets a
          > bit
          > messy since I would need to support the "primitive" classes, like String,
          > which
          > extend Object.
          >
          > I've added an implementation to Object in Global.as and this goes as far
          > as
          > tricking the compiler. However during execution a method not found
          > exception is
          > thrown. Looking at Global.as it seems that this file doesn't contain
          > implementations of methods.
          >
          > Anyone have an idea of where the implementation for the library classes
          > are?
          > Perhaps in the player itself? Additionally, am I running the risk of
          > player
          > version incompatibilities?
          >


          • 2. Re: Altering Object class source
            Slash_Wannabe Level 1
            Yeah there is ObjectUtils.compare but that's not what I'm looking for. That method compares all properties of the object which may or may not be what the object defines as equal. For example, two people objects may have the same id property but a different name property. In this example I would want these two people objects to be equal because they have the same id. Using ObjectUtils.compare would mean they aren't equal. I know you can specify what properties to exclude from the compare but that's just a maintenance nightmare.

            Alternatively I could pass around equality functions but this again gets pretty messy. The most elegant solution is to use equal methods.
            • 3. Re: Altering Object class source
              Level 7

              "Slash Wannabe" <webforumsuser@macromedia.com> wrote in message
              news:gfu27m$hj7$1@forums.macromedia.com...
              > Yeah there is ObjectUtils.compare but that's not what I'm looking for.
              > That
              > method compares all properties of the object which may or may not be what
              > the
              > object defines as equal. For example, two people objects may have the same
              > id
              > property but a different name property. In this example I would want these
              > two
              > people objects to be equal because they have the same id. Using
              > ObjectUtils.compare would mean they aren't equal. I know you can specify
              > what
              > properties to exclude from the compare but that's just a maintenance
              > nightmare.
              >
              > Alternatively I could pass around equality functions but this again gets
              > pretty messy. The most elegant solution is to use equal methods.

              I'd suggest you pick up Actionscript 3 Design Patterns and look at what they
              have to say about "abstract" classes.

              HTH;

              Amy


              • 4. Re: Altering Object class source
                *gsb* Level 1
                Well, without changing the Object class, you could add a class of your own as a "global object compare utility function." One instance, always available.

                Use a singleton pattern, say class 'myObjUtils', with one static function; your Object compare function. Would use like:

                var results:Boolean = myObjUtils.getInstance().compare( Obj1, Obj2 );

                • 5. Re: Altering Object class source
                  Slash_Wannabe Level 1
                  You are certainly right but that's a workaround and it's basically the same as using ObjectUtils .

                  I want to work with inheritance hence wanting to fix the object class.
                  • 6. Altering Object class source
                    *gsb* Level 1
                    Well, can you not just take a copy of the original Object.as class, modify it and add it prior to the main libs in the Flex compiler directives? That way it overrides the standard Object class, no?

                    I did something similar for an MX class once and it worked fine. Best practices ... maybe not.



                    • 7. Re: Altering Object class source
                      *gsb* Level 1
                      So I made an example that works if I understand your question properly.
                      It adds the 'equals' prototype to the Object class.
                      Extenders of the Object class can, sort of, 'override' the default or not and use the default.