Question

One of the things I miss the most in ActionScript is the lack of operator overloading, in particular ==. I kind of work around this issue by adding a "Compare" method to my classes, but that doesn't help in many cases, like when you want to use things like the built in Dictionary.

Is there a good way to work around this problem?

Was it helpful?

Solution

Nope.

But it doesn't hurt to add equals methods to your own classes. I try to never use == when comparing objects (the same goes for ===, which is the same thing for objects) since it only checks identity .

Sadly all the collections in Flash and Flex assume that identity is the only measure of equality that is needed.

There are hints in Flex that someone wanted to alleviate this problem at one time, but it seems like it was abandoned: there is an interface called IUID, and it is mentioned in the Flex Developer's Guide , but it is not used anywhere. Not even the collections in Flex use it to determine equality. And since you are asking for a solution for Flash, it may not have helped you anyway.

I've written some more about this (in the context of Flex) on my blog: Is there no equality?.

OTHER TIPS

Yes it can be done (but be careful, its hacky): http://filimanjaro.com/2012/operators-overloading-in-as3-javascript-too-%E2%80%93-workaround/

In the tutorial I wrote about +=, -= operator overloading. But it's also possible with ==, I can write more about that if it's not clear.

Ah, the approach has some drawback (in rare cases it can be even dangerous). Think twice, before using it in a production.

EDIT:

After tests it seems the trick with +=, -= doesn't apply to == operator (what makes sense). Sorry for misleading info.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top