Maps and Lists

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Maps and Lists

Uwe Hartl
Hello,
would it be a bad thing to have this working?

def list = [90, "string"]
println list.getClass().toString() + "s String representation is: " + list.toString()
def map = ["test1":90, "test2":"string"]
println map.getClass().toString() + "s String representation is: " + map.toString()

def shell = new GroovyShell()
def result = shell.evaluate(map.toString())  // does not work
def result = shell.evaluate(list.toString())    // does not work


This would make it easy to save a status (as long as numbers and strings are stored in the lists and maps) and reload.

How do yu think about this?

Uwe
Reply | Threaded
Open this post in threaded view
|

Re: Maps and Lists

Jochen Theodorou
Uwe Hartl schrieb:

> Hello,
> would it be a bad thing to have this working?
>
> def list = [90, "string"]
> println list.getClass().toString() + "s String representation is: " +
> list.toString()
> def map = ["test1":90, "test2":"string"]
> println map.getClass().toString() + "s String representation is: " +
> map.toString()
>
> def shell = new GroovyShell()
> def result = shell.evaluate(map.toString())  // does not work
> def result = shell.evaluate(list.toString())    // does not work
>
>
> This would make it easy to save a status (as long as numbers and strings are
> stored in the lists and maps) and reload.
>
> How do yu think about this?

People expect from getClass() to return a Class. In case of the List,
the result would be a list or array of classes. So you would have to
know you are working on a list. But if you already have to know that,
then why use getClass? Another method name can do that then. It is
basically the same for maps, only that you here get a 2d array or list...

bye blackdrag

--
Jochen Theodorou
Groovy Tech Lead
http://blackdragsview.blogspot.com/

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Maps and Lists

tugwilson
In reply to this post by Uwe Hartl

On 10 Sep 2006, at 20:24, Uwe Hartl wrote:

>
>
> This would make it easy to save a status (as long as numbers and  
> strings are
> stored in the lists and maps) and reload.
>
> How do yu think about this?


so you are proposing that toString on Map and List should return the  
groovy representation of the Map ir String? (I'm afraid you post  
wasn't very clear on this)

def x = [a: 1, b:  2, c: 3]
x.d = x

println x.toString()

what do I get printed here?


John Wilson
The Wilson Partnership
web http://www.wilson.co.uk
blog http://eek.ook.org



---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Maps and Lists

Uwe Hartl
Yes, you are right. I mean, I would like to be able to execute the map.toString - String and get back the map. Sorry for writing so unclear.

Your example is a funny one. Why would somebidy store a reference to the map itself in the map? The question here arises, if the memory used by this map gets released if all external references are deleted...  If not, this would mean a possibility to consume memory and never be able to release it.



John Wilson wrote
On 10 Sep 2006, at 20:24, Uwe Hartl wrote:

>
>
> This would make it easy to save a status (as long as numbers and  
> strings are
> stored in the lists and maps) and reload.
>
> How do yu think about this?


so you are proposing that toString on Map and List should return the  
groovy representation of the Map ir String? (I'm afraid you post  
wasn't very clear on this)

def x = [a: 1, b:  2, c: 3]
x.d = x

println x.toString()

what do I get printed here?


John Wilson
The Wilson Partnership
web http://www.wilson.co.uk
blog http://eek.ook.org



---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email
Reply | Threaded
Open this post in threaded view
|

Re: Maps and Lists

Uwe Hartl
In reply to this post by Jochen Theodorou
Sorry I was unclear, John got my question correct.

Reply | Threaded
Open this post in threaded view
|

Re: Maps and Lists

tugwilson
In reply to this post by Uwe Hartl

On 11 Sep 2006, at 19:25, Uwe Hartl wrote:

>
> Your example is a funny one. Why would somebidy store a reference  
> to the map
> itself in the map? The question here arises, if the memory used by  
> this map
> gets released if all external references are deleted...  If not,  
> this would
> mean a possibility to consume memory and never be able to release it.


Why would you not store a reference to a Map in itself? It's just a  
simple example of a more general problem. Maps and Lists can and do  
hold cyclic references. If you want to do serialisation of these data  
structure you have to be ably to handle this.

The problem of garbage collection these sort of structure was solved  
many years ago. The Java GC will have no problem recovering the storage.


John Wilson
The Wilson Partnership
web http://www.wilson.co.uk
blog http://eek.ook.org



---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Maps and Lists

Uwe Hartl
Ok, thanks, we can close this thread. I found the function toMapString() which does exactly what I need.

Uwe
Reply | Threaded
Open this post in threaded view
|

Re: Maps and Lists

Martin C. Martin-2
In reply to this post by tugwilson


John Wilson wrote:
>
> Why would you not store a reference to a Map in itself? It's just a
> simple example of a more general problem. Maps and Lists can and do hold
> cyclic references. If you want to do serialisation of these data
> structure you have to be ably to handle this.
>
> The problem of garbage collection these sort of structure was solved
> many years ago. The Java GC will have no problem recovering the storage.

See for example:

http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29

It talks a lot about cycles.

- Martin

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email