Quantcast

Why has groovy no built-in tuple support (like map and lists)?

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Why has groovy no built-in tuple support (like map and lists)?

aldana-2
i love the language-integrated support of maps and lists in groovy -> [:] and []

i wonder why tuple semantics aren't also included. is there a reason for that? following syntax would be very handy:
def tuple1 = (24,4)
def tuple2 = (2,5,3)


just want to backup that there is no showstopper reason before opening such a request in jira (couldn't find such a 'tuple support' request-ticket).
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why has groovy no built-in tuple support (like map and lists)?

Alexander Veit
> i love the language-integrated support of maps and lists in
> groovy -> [:] and
> []
>
> i wonder why tuple semantics aren't also included. is there a
> reason for
> that? following syntax would be very handy:
> def tuple1 = (24,4)
> def tuple2 = (2,5,3)

What is the difference between tuples and lists?

def tuple = ["foo", "bar"]
def (foo, bar) = tuple

assert foo == "foo"
assert bar == "bar"


--
Cheers,
Alex


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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why has groovy no built-in tuple support (like map and lists)?

Nathan Stehr
Well, in python, the main difference between lists and tuples is that
tuples are immutable.  This is the first thing that pops to mind, but
I'm not sure if this is what Manuel had in mind when he posted his
original question.

Nathan

On Mon, Dec 28, 2009 at 9:56 AM, Alexander Veit <[hidden email]> wrote:

>> i love the language-integrated support of maps and lists in
>> groovy -> [:] and
>> []
>>
>> i wonder why tuple semantics aren't also included. is there a
>> reason for
>> that? following syntax would be very handy:
>> def tuple1 = (24,4)
>> def tuple2 = (2,5,3)
>
> What is the difference between tuples and lists?
>
> def tuple = ["foo", "bar"]
> def (foo, bar) = tuple
>
> assert foo == "foo"
> assert bar == "bar"
>
>
> --
> Cheers,
> Alex
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why has groovy no built-in tuple support (like map and lists)?

aldana-2
yes internally they could be immutable lists (order of elements cannot be changed). but i am more referring to the syntax level.

currently in groovy i find it lengthy to write:
def t=new Tuple(23,2,4)

it would be nicer to have:
def t=(23,2,4) //shortcut for new Tuple(....)

python also offers [] for lists and () for tuples.


Nathan Stehr wrote
Well, in python, the main difference between lists and tuples is that
tuples are immutable.  This is the first thing that pops to mind, but
I'm not sure if this is what Manuel had in mind when he posted his
original question.

Nathan

On Mon, Dec 28, 2009 at 9:56 AM, Alexander Veit <alexander.veit@gmx.net> wrote:
>> i love the language-integrated support of maps and lists in
>> groovy -> [:] and
>> []
>>
>> i wonder why tuple semantics aren't also included. is there a
>> reason for
>> that? following syntax would be very handy:
>> def tuple1 = (24,4)
>> def tuple2 = (2,5,3)
>
> What is the difference between tuples and lists?
>
> def tuple = ["foo", "bar"]
> def (foo, bar) = tuple
>
> assert foo == "foo"
> assert bar == "bar"
>
>
> --
> Cheers,
> Alex
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

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

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why has groovy no built-in tuple support (like map and lists)?

Robert Fletcher
But is there some important difference between

    def tuple = [23, 2, 4]

and what you're proposing? If immutability is important then Groovy
decorates collections with .asImmutable()

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why has groovy no built-in tuple support (like map and lists)?

Merlyn Albery-Speyer
> But is there some important difference between
>
>    def tuple = [23, 2, 4]
>
> and what you're proposing?

A tuple isn't much more than just syntax sugar built around an array
IMO. Being able to assign to variables directly from a tuple is also
handy, e.g.: (x,y,z) = (1,2,3)

> If immutability is important then Groovy
> decorates collections with .asImmutable()

Yet the same argument can be made for the Groovy map syntax. Without
it we'd still be able to create maps, just not in a way that would
change our coding behaviour. (23, 2, 4) is a lot more readable, and
therefor more conducive to use, than [23, 2, 4].asImmutable().

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why has groovy no built-in tuple support (like map and lists)?

aldana-2
In reply to this post by Robert Fletcher
i think from readability and expressiveness yes.

when i read code i identify a (x,y,z) instantly with tuple semantics. [x,y,z].asImmutable() is less expressive (it shows a implementation detail, how a tuple could be enforced) and is more verbose.


Robert Fletcher wrote
But is there some important difference between

    def tuple = [23, 2, 4]

and what you're proposing? If immutability is important then Groovy
decorates collections with .asImmutable()

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

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why has groovy no built-in tuple support (like map and lists)?

Merlyn Albery-Speyer
I think the main question for debate is how badly is it hurting us not
to have tuples? I don't think its absence is causing much pain, yet it
would be a cool feature to have.

On Mon, Dec 28, 2009 at 10:33 AM, aldana <[hidden email]> wrote:

>
> i think from readability and expressiveness yes.
>
> when i read code i identify a (x,y,z) instantly with tuple semantics.
> [x,y,z].asImmutable() is less expressive (it shows a implementation detail,
> how a tuple could be enforced) and is more verbose.
>
>
>
> Robert Fletcher wrote:

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why has groovy no built-in tuple support (like map and lists)?

Peter Niederwieser
In reply to this post by Nathan Stehr
The difference is more pronounced in statically typed languages, where every element of a tuple can have a different static type. Not sure if it is worth to add new literal syntax for immutability alone.

Related dev question: Are Groovy's list and map literals (or, in other words, ArrayList and LinkedHashMap) guaranteed to be thread-safe if they are never modified after publication? This is a tricky question, and I don't know the answer.

Cheers,
Peter

Nathan Stehr wrote
Well, in python, the main difference between lists and tuples is that
tuples are immutable.  This is the first thing that pops to mind, but
I'm not sure if this is what Manuel had in mind when he posted his
original question.

Nathan

On Mon, Dec 28, 2009 at 9:56 AM, Alexander Veit <alexander.veit@gmx.net> wrote:
>> i love the language-integrated support of maps and lists in
>> groovy -> [:] and
>> []
>>
>> i wonder why tuple semantics aren't also included. is there a
>> reason for
>> that? following syntax would be very handy:
>> def tuple1 = (24,4)
>> def tuple2 = (2,5,3)
>
> What is the difference between tuples and lists?
>
> def tuple = ["foo", "bar"]
> def (foo, bar) = tuple
>
> assert foo == "foo"
> assert bar == "bar"
>
>
> --
> Cheers,
> Alex
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

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

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why has groovy no built-in tuple support (like map and lists)?

kodeninja
In reply to this post by Robert Fletcher

Robert Fletcher wrote
But is there some important difference between

    def tuple = [23, 2, 4]

and what you're proposing? If immutability is important then Groovy
decorates collections with .asImmutable()
Robert, asImmutable() still allows the order of the list to be altered. Groovy allows that!

def immutableList = [1,2,3,4].asImmutable()
println  immutableList.reverse() // prints [4,3,2,1]. No exception here.

In fact, even using Collections.unmodifiableList() allows the list to be reversed.

def unmodifiableList = Collections.unmodifiableList([1,2,3,4])
println unmodifiableList.reverse() // prints [4,3,2,1]. No exception here.

Doing the latter in Java throws UnsupportedOperationException!

Additions/Removals are not allowed under Groovy also, but re-ordering seems to be allowed...

-kodeninja
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why has groovy no built-in tuple support (like map and lists)?

Guillaume Laforge-4
Be careful: reverse() creates a new list!
It's not reordering your initial list, since it's creating a new one.

On Thu, Dec 31, 2009 at 13:11, kodeninja <[hidden email]> wrote:

>
>
>
> Robert Fletcher wrote:
>>
>> But is there some important difference between
>>
>>     def tuple = [23, 2, 4]
>>
>> and what you're proposing? If immutability is important then Groovy
>> decorates collections with .asImmutable()
>>
>>
>
> Robert, asImmutable() still allows the order of the list to be altered.
> Groovy allows that!
>
> def immutableList = [1,2,3,4].asImmutable()
> println  immutableList.reverse() // prints [4,3,2,1]. No exception here.
>
> In fact, even using Collections.unmodifiableList() allows the list to be
> reversed.
>
> def unmodifiableList = Collections.unmodifiableList([1,2,3,4])
> println unmodifiableList.reverse() // prints [4,3,2,1]. No exception here.
>
> Doing the latter in Java throws UnsupportedOperationException!
>
> Additions/Removals are not allowed under Groovy also, but re-ordering seems
> to be allowed...
>
> -kodeninja
> --
> View this message in context: http://old.nabble.com/Why-has-groovy-no-built-in-tuple-support-%28like-map-and-lists%29--tp26943780p26977592.html
> Sent from the groovy - user mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>



--
Guillaume Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why has groovy no built-in tuple support (like map and lists)?

kodeninja
In reply to this post by kodeninja
kodeninja wrote
Robert, asImmutable() still allows the order of the list to be altered. Groovy allows that!

def immutableList = [1,2,3,4].asImmutable()
println  immutableList.reverse() // prints [4,3,2,1]. No exception here.

In fact, even using Collections.unmodifiableList() allows the list to be reversed.

def unmodifiableList = Collections.unmodifiableList([1,2,3,4])
println unmodifiableList.reverse() // prints [4,3,2,1]. No exception here.

Doing the latter in Java throws UnsupportedOperationException!

Additions/Removals are not allowed under Groovy also, but re-ordering seems to be allowed...

-kodeninja

Hmm.. on further poking around, I found that List.reverse() actually returns a new list. So, Groovy actually isn't modifying the immutable list, but returning a new list!

In fact, if I use java.util.Collections.reverse(unmodifiableList) in Groovy, this too throws an exception.
My bad...

-kodeninja
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why has groovy no built-in tuple support (like map and lists)?

kodeninja
In reply to this post by Guillaume Laforge-4
Guillaume, you beat me to that :). I too realized the mistake...

Guillaume Laforge-2 wrote
Be careful: reverse() creates a new list!
It's not reordering your initial list, since it's creating a new one.

On Thu, Dec 31, 2009 at 13:11, kodeninja <koffee.2.kode@gmail.com> wrote:
>
>
>
> Robert Fletcher wrote:
>>
>> But is there some important difference between
>>
>>     def tuple = [23, 2, 4]
>>
>> and what you're proposing? If immutability is important then Groovy
>> decorates collections with .asImmutable()
>>
>>
>
> Robert, asImmutable() still allows the order of the list to be altered.
> Groovy allows that!
>
> def immutableList = [1,2,3,4].asImmutable()
> println  immutableList.reverse() // prints [4,3,2,1]. No exception here.
>
> In fact, even using Collections.unmodifiableList() allows the list to be
> reversed.
>
> def unmodifiableList = Collections.unmodifiableList([1,2,3,4])
> println unmodifiableList.reverse() // prints [4,3,2,1]. No exception here.
>
> Doing the latter in Java throws UnsupportedOperationException!
>
> Additions/Removals are not allowed under Groovy also, but re-ordering seems
> to be allowed...
>
> -kodeninja
> --
> View this message in context: http://old.nabble.com/Why-has-groovy-no-built-in-tuple-support-%28like-map-and-lists%29--tp26943780p26977592.html
> Sent from the groovy - user mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>



--
Guillaume Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one

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

    http://xircles.codehaus.org/manage_email

Loading...