views:

45

answers:

3

Python's repr function is awesome: it returns a printable representation of an object.

For example, repr(["a'b", {1: 2}, u"foo"]) is the string '["a\'b", {1: 2}, u\'foo\']'. Notice, eg, how quotes are properly escaped.

So, is there anything like this for ActionScript?

For example, right now: [1, 2, ["3", "4"]].toString() produces the string "1,2,3,4"… Which really isn't very helpful. I'd like it to produce a string like… Well, '[1, 2, ["3", "4"]]'.

I have considered using a JSON library… But that's less than ideal, because it will try to serialize instances of arbitrary objects, which I don't really want.

A: 

This is the only thing remotley close:

valueOf ()

public function valueOf():Object

Language Version : ActionScript 3.0 Runtime Versions : AIR 1.0, Flash Player 9

Returns the primitive value of the specified object. If this object does not have a primitive value, the object itself is returned.

Note: Methods of the Object class are dynamically created on Object's prototype. To redefine this method in a subclass of Object, do not use the override keyword. For example, A subclass of Object implements function valueOf():Object instead of using an override of the base class.

Returns Object — The primitive value of this object or the object itself.

Todd Moses
Unfortunately, this doesn't help. After reading the docs, it's not entirely clear what the purpose of `valueOf` is… But this isn't it. Just for example: `([1, 2, ["3", "4"]]).valueOf() == "1,2,3,4`.
David Wolever
+1  A: 

AFAIK there isn't any quick-easy one line command that does what you want, but here's a way to do it, straight from Adobe I might add

http://livedocs.adobe.com/flex/3/html/help.html?content=usingas_8.html

invertedSpear
Hhmm… While `ObjectUtil.toString(…)` doesn't quite do what I want - it's really verbose - it's good to know about. Thanks.
David Wolever
Yeah the ObjectUtil.toString() kinda sucks. but you can use the loop examples to get your own dump function going, add a little recursion and you're good to go, or at least as good as you can be.
invertedSpear
A: 

You can try the ObjectUtil.toString function, it's not exatly what you want, but I don't think you will find anything closer to what you want as it's functions is described as "Pretty-prints the specified Object into a String.", which is what it does, but keeps much more info that you would want. As Array is a complex data object and that's why it annotates it like that.

    var a:Array = [1, 2, ["3", "4"]];
    trace (ObjectUtil.toString(a));
    // returns
    // (Array)#0
    //  [0] 1
    //  [1] 2
    //  [2] (Array)#1
    //    [0] "3"
    //    [1] "4"

I'm wondering how would repr handle this example:

    var a:Array = [0,1,2];
a.push(a);                  
trace (ObjectUtil.toString(a));
    // returns
    // (Array)#0
    //   [0] 0
    //   [1] 1
    //   [2] 2
    //   [3] (Array)#0
Robert Bak