A different use for custom namespaces (1/2)

When namespaces were first introduced in Actionscript 3 I thought it was all pretty cool, but still I sort of had a difficult time thinking of actual use-cases.

What I initially would have liked to use custom namespaces for was to restrict access to certain properties/methods, to only be accessible for certain classes, in the way that private and protected enable you to — unfortunately there is no simple way of defining who can import your namespace, consequently rendering namespaces somewhat useless for this particular use-case.

Then some time ago it struck me that rather than defining new members with a custom namespace, that you could of course also enhance an existing method instead (I admit this might seem like a no-brainer, but it took me a while to bump into).

Let’s say you have a subclass that for regular (outside) usage needs to override a method in the superclass, but itself still needs to use the original (super-) method. You would probably just override the method in the subclass and use super.method() wherever you needed to use the original functionality.
Now let’s say that you need to make a subclass of your subclass, which would also require access to the original functionality in said method. All of a sudden super.method() will not work for you anymore.

Here’s where namespaces start coming in handy, while still simply overriding the method in the first subclass, you also implement a function with the same name but with your own namespace (for example “original”). Then whenever you need to call the original implementation you can call original::method(), regardless of how deep down in the inheritance-chain you are.

This is only one use-case, you could for instance also use this concept to work around the nasty ambiguous-reference bug you get (or used to get) when declaring a getter and a setter with different (native) namespaces.

To keep the focus on the above described concept I put the example-code in a seperate post, feel free to read on though.

7 thoughts on “A different use for custom namespaces (1/2)

  1. Pingback: Shark’s Pond » ?????????

Comments are closed.