a slice of slices

499 views
Skip to first unread message

Alex Combas

unread,
Dec 23, 2009, 9:38:40 PM12/23/09
to golang-nuts

[][]int{[]int{130,84}, []int{15,46}, []int{44,50}}


This seems like an awfully large amount of stuttering to me.

Is there a simpler way to represent my data?

Ideally I wish I could just go {{130,84},{15,46},{44,50}}


Best regards,
Alex Combas

Rob 'Commander' Pike

unread,
Dec 24, 2009, 12:49:57 AM12/24/09
to Alex Combas, golang-nuts

On 24/12/2009, at 1:38 PM, Alex Combas wrote:

>
> [][]int{[]int{130,84}, []int{15,46}, []int{44,50}}
>
>
> This seems like an awfully large amount of stuttering to me.

Yes it is, and we spent some time talking about these inner type
specifiers. The problem is that if you take them out, in general the
parsing and execution of initializers like these gets pretty messy, so
we decided to require them with the understanding that they could
potentially be removed if the the syntax got sweeter.

> Is there a simpler way to represent my data?
>
> Ideally I wish I could just go {{130,84},{15,46},{44,50}}

Yes, but what type is that object? It's clear to you, but is it clear
to the compiler? In general it could initialize a lot of different
types.

-rob

eadfrith

unread,
Dec 24, 2009, 12:55:47 AM12/24/09
to golang-nuts

> > Ideally I wish I could just go {{130,84},{15,46},{44,50}}
>
> Yes, but what type is that object?  It's clear to you, but is it clear  
> to the compiler?  In general it could initialize a lot of different  
> types.

what about [][]int{{130,84},{15,46},{44,50}}

This is close to the equivalent Java new int[][]{{130,84},{15,46},
{44,50}}

Rob 'Commander' Pike

unread,
Dec 24, 2009, 3:07:01 AM12/24/09
to eadfrith, golang-nuts

That helps but what are the types of the inner pieces? You need to
dig into the structure and derive types internally. It's certainly
possible to do this but it's harder than it looks (I speak from
experience) to get it right in all cases. It's also harder than it
looks to define just what is and what is not legal in initializers if
you go this way.

There's no question Go takes the simple route, but I explained why in
my previous message.

-rob

Alex Combas

unread,
Dec 24, 2009, 10:02:56 PM12/24/09
to Rob 'Commander' Pike, eadfrith, golang-nuts
On Thu, Dec 24, 2009 at 12:07 AM, Rob 'Commander' Pike <r...@google.com> wrote:

On 24/12/2009, at 4:55 PM, eadfrith wrote:


Ideally I wish I could just go {{130,84},{15,46},{44,50}}

Yes, but what type is that object?  It's clear to you, but is it clear
to the compiler?  In general it could initialize a lot of different
types.

what about [][]int{{130,84},{15,46},{44,50}}

This is close to the equivalent Java new int[][]{{130,84},{15,46},
{44,50}}

That helps but what are the types of the inner pieces?  

They would be type []int since Go does not allow mixed slices and since
[][]int was used to declare the slice they therefore can only be the type of []int.

 
You need to dig into the structure and derive types internally.  It's certainly possible to do this but it's harder than it looks (I speak from experience) to get it right in all cases.  It's also harder than it looks to define just what is and what is not legal in initializers if you go this way.

There's no question Go takes the simple route, but I explained why in my previous message.


From the previous post you said "they could potentially be removed if the the syntax got sweeter."

So I am curious now: Do you have plans to sweeten the syntax for array slices?
Reply all
Reply to author
Forward
0 new messages