15

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?

Andres
  • 1,875
  • 1
  • 19
  • 28

2 Answers2

12

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?.

Theo
  • 131,503
  • 21
  • 160
  • 205
  • This isn't a problem. Low level collections should use instance equality. Any other type of equality check is completely arbitrary and should be well-defined in an equals method. It should be very obvious whether you want instance equality or some arbitrary data equality check when you're coding something. In general, the only time you'd want data equality checks (checks of a particular object key) is when dealing with remote data objects or database records that are identifiable by a particular key. There is no generic implementation in collections, because it's trivial to implement. – Triynko May 13 '14 at 21:19
  • I just created a collection of helper methods in a DataUtils class with a method signature like `indexOfValue( collection:*, value:*, match_field_or_function:* ):int;` The method can simply iterate over the collection and either extract a particular field from each object for comparison with *value*, or can run each object through a function whose result is compared with *value*. The *match_field_or_function* parameter even handles multi-part field names for drilling down such as "data.id" on a collection of list items that each have a data property holding any kind of object with an id field. – Triynko May 13 '14 at 21:32
4

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.

average Joe
  • 4,377
  • 2
  • 25
  • 23