Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

pretty-print a map?

adamohernadamohern Member, OS Professional Posts: 216 PRO



The above is really hard on the eyes. I find it difficult to read. Is there a way to print out nested maps and arrays with better formatting?

Comments

  • kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565
    In FeatureScript, it's beneficial to define types for the data you're using. This lets you write functions that accept only that type, and overload functions for that type — including toString().

    So something like:
    // new type
    export type MyType typecheck isMyType;
    export predicate isMyType(value)
    {
        value is map;
        isInteger(value.id);
        isLength(value.length);
        is3dLengthVector(value.endPoint);
        isAngle(value.axialRotation);
        is3dDirection(value.orientation);
    }
    
    // constructor
    export function myType(id is number, length is ValueWithUnits, endPoint is Vector,
        axialRotation is ValueWithUnits, orientation is Vector) returns MyType
    {
        return {"id" : id, "length" : length, "endPoint" : endPoint,
            "axialRotation" : axialRotation, "orientation" : orientation } as MyType;
    }
    
    // overloads
    export function toString(value is MyType) returns string
    {
        var out is string = "MyType:";
        for (var entry in value)
        {
            out ~= "\n" ~ entry.key ~ ": ";
            out ~= toString(entry.value);        
        }
        return out;
    }
    
    export function debug(value is MyType)
    {
        // call debug() on the relevant data in this type
    }
    
    

    I'm not sure about your exact use case, but for sake of conciseness in the type, it might be nicer to group endPoint, axialRotation, and orientation into a single CoordSystem to start with (personally, I find it mind-numbing trying to do vector math unless I'm starting with a CoordSystem and can call fromWorld() everywhere).

    In the future, we'd like to have a nicer way of presenting big chunks of native data by default (even when custom types aren't defined), but today, untyped data is printed as a big long literal.
  • kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565
    Update: I misinterpreted the state of std. Turns out, we do have a decent toString() method defined on maps. The trick is to actually call toString, before string concatenation.
    println("my map is " ~ toString(myMap));
    // or simply
    println(myMap);
    
    Making a type is still good practice, but for the pretty-printing, calling the preexisting toString() will be much better than what you're seeing above.

Sign In or Register to comment.