-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use ByteArray serialization to compare 2 class instances
- Loading branch information
1 parent
f0d92ca
commit db2c33c
Showing
1 changed file
with
7 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db2c33c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't work.
ByteArray
serialization will strip out attributes tagged with the[Transient]
metadata tag. If these attributes' values differ, the comparison will still claim they're the same.db2c33c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right ; but do we necessary have to handle all the complexity of ActionScript 3 / Flex for a library which targets simplicity ?
Personnnaly I can live without a full-featured
_.isEqual
function, but in some cases it may be useful to have a more robust implementation, able to deal with these[Transient]
metadata tag for example.Do you want to try to write a cleaner
_.isEqual
AS3 function ?db2c33c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the implementation isn't robust, it should be removed. The original underscore.js targets both simplicity and robustness - see the rationale against a deep copy method here.
I've used
[Transient]
tags to prevent certain public attributes from being serialized, instead of writing a method to strip out all these (possibly-deeply-nested) attributes._.isEqual
would be broken on every instance of these classes.db2c33c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not to mention...classes that use private/protected attributes can't be properly compared, e.g.
Unless there's some way to subvert access modifiers, I don't think it's possible to implement a bulletproof
_.isEqual
method in AS3.db2c33c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we only can access public properties, and have to compare class instances with the informations we have access to.
The same problem occur in Javascript if you have "private" properties in functions :
But this doesn't mean we can't use
_.isEqual
in Javascript :-)Whatever language you use, all we have to know is that class instances can only be compared by comparing their pubic properties.
About the
[Transient]
tag : my personnal advice would be to add aisEqual()
method on classes you want to compare which use this tag. This is a trick that the vanilla Underscore.js have - and Underscore.as have it too.db2c33c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test will fail...I'm guessing
ByteArray.writeObject
serializes an object in the order in which its entries are defined, so comparing thetoString()
s of the two will show that they're different.I think it's too risky to depend on
ByteArray
...there seem to be a lot of gotchas. _.isEqual should be removed until a robust implementation that does deep traversals and comparisons on built-in objects is written.