Thibault Kruse
2015-02-22 14:00:01 UTC
Hi,
currently groovysh does not handle recursive datastructures too well.
This works:
groovy:000> d = [:]
===> [:]
groovy:000> d.put(1, d)
===> null
groovy:000> d
[1: (this map)]
but this does not:
groovy:000> d = [:]
===> [:]
groovy:000> d.put(1, [d])
===> null
groovy:000> d
null // or StackOverflowError displayed, depending on Groovy Version
The class https://github.com/groovy/groovy-core/blob/master/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
is responsible for the rendering of structures (since java's
toString() is not used).
There are several ways to handle this, one is to catch
StackOverFlowError (either deep in the recursion or at the top).
Another way is to leave things as they are now.
The last way is to properly detect cycles, meaning each recursive
invocation gets a List of all Objects already in the process of being
rendered, and so method calls produce a placeholder token for any
element being rendered recursively. This would mean adding a new
function like
private static String formatWithouCycles(Object arguments, boolean
verbose, int maxSize, Collection itemsInProgress) {...}
and extending all the private static methods to honor the collection.
And some placeholder token would be required '(this map)' would not do anymore.
Thoughts?
regards,
Thibault
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
currently groovysh does not handle recursive datastructures too well.
This works:
groovy:000> d = [:]
===> [:]
groovy:000> d.put(1, d)
===> null
groovy:000> d
[1: (this map)]
but this does not:
groovy:000> d = [:]
===> [:]
groovy:000> d.put(1, [d])
===> null
groovy:000> d
null // or StackOverflowError displayed, depending on Groovy Version
The class https://github.com/groovy/groovy-core/blob/master/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
is responsible for the rendering of structures (since java's
toString() is not used).
There are several ways to handle this, one is to catch
StackOverFlowError (either deep in the recursion or at the top).
Another way is to leave things as they are now.
The last way is to properly detect cycles, meaning each recursive
invocation gets a List of all Objects already in the process of being
rendered, and so method calls produce a placeholder token for any
element being rendered recursively. This would mean adding a new
function like
private static String formatWithouCycles(Object arguments, boolean
verbose, int maxSize, Collection itemsInProgress) {...}
and extending all the private static methods to honor the collection.
And some placeholder token would be required '(this map)' would not do anymore.
Thoughts?
regards,
Thibault
x = {}
x[1] = x
x
{1: {...}}x[1] = x
x
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email