Odd EReg Behaviour

72 views
Skip to first unread message

Arnim Schachtschabel

unread,
Aug 24, 2016, 12:15:15 PM8/24/16
to Haxe
Hello Community,

when I convert an object instance to a string with    Std.string()
   and when I try to apply an EReg on it, to find what is inside the curly brackets, only crap comes out.

I have simplified this phänomenon by simply applying an EReg that is supposed to show the whole string.

Applying this to an object representation, it just returns one curly bracket {   , while applying it to the same string that was manually written, it returns the complete string, as it is supposed to.

See my example.

Does anybody can tell me, what I am doing wrong?
Technically? Conceptionally? Logically?

Kind regards
Arnim

What I am intending to do:

I want to compare instanced objects. The mashine is only supposed to compare the equality of the values of each instance. No need to know if they are the same instances.
So far, so good and so easy.
Simply make them a string and compare them:

if (Std.string( objX )  ==  Std.string( objY ) )
doSomething();

But regrattably it is not as simple as that.
If the order of initializations of the fields differs, also the resulting strings differ:


objX.int = 13
objX.string = "Great"
Std.string( objX )

leads to-->       { int : 13, string : Great }

And
objY.string = "Great"
objY.int = 13
Std.string( objY)

leads to-->       { string : Great,  int : 13 }
So, although the value of each field is equal, the comparison would return 'false'.


Munir Hussin

unread,
Aug 25, 2016, 5:15:14 PM8/25/16
to Haxe
If you intend to compare objects by their fields, using using Std.string() may not work as expected, especially when there are circular references.

var objX:Dynamic = {
    i: 5,
    a: null,
};

var objY:Dynamic = {
    a: null,
    i: 5,
};

// circular reference
objX.a = objY;
objY.a = objX;

// doesn't work well
trace(Std.string(objX)); // { i : 5, a : { a : { i : 5, a : { a : { i : <...>, a : <...> }, i : 5 } }, i : 5 } }
trace(Std.string(objY)); // { a : { i : 5, a : { a : { i : 5, a : { a : <...>, i : <...> } }, i : 5 } }, i : 5 }

You can try haxe.Serializer with USE_CACHE set to true, but the output can be different, like you mentioned:

Serializer.USE_CACHE = true;
trace(Serializer.run(objX)); // oy1:ii5y1:aoR1r0R0i5gg
trace(Serializer.run(objY)); // oy1:aoy1:ii5R0r0gR1i5g

Perhaps a deepEquals recursive method with caching? When you encounter an object, make sure to sort the fields.
Message has been deleted

Clark Jones

unread,
Aug 25, 2016, 10:07:21 PM8/25/16
to Haxe
Would it be unrealistic in your real use case(not the example) to add a toString method that outputs the property values of the instantiated object in a predictable manner. ex:  public function toString() {  return 'int:$int,string:$string'; }   
Message has been deleted

Clark Jones

unread,
Aug 25, 2016, 10:41:23 PM8/25/16
to Haxe
http://try.haxe.org/#64545 updated copy of your example

Arnim Schachtschabel

unread,
Aug 26, 2016, 8:48:11 AM8/26/16
to Haxe
Hallo Munir, hello Clark,

thank you both for your proposals.

Now, I have developed a work-around that even makes the code a little bit more efficient on the other side. That reconciles me with the higher effort it took.

Your solutions are the best choice in specific cases and for specific types. But regrattably they do not allow the comparison between two instances of the same type if the comparing method is not explicitely prepared for that type.

I was hoping for a more simple solution.

But you offered me another perspective to the problem. So, your answers helped as well.

And good things are never easy...
;-)

Kind regards
Arnim

Arnim Schachtschabel

unread,
Aug 26, 2016, 9:05:11 AM8/26/16
to Haxe
Update:

@Clark: Your answer inspired me to work out something with Reflect. ... iterating the fieldnames and then putting them into a strictly determined order. And then having a static method, that works like your solution.

But for the time being, the work-around will do.

So, thanks again you both.
Reply all
Reply to author
Forward
0 new messages