FAQ
Tuples are essentially just arrays... or structs... depending on what
you're trying to do. If you consider something like python, where you can
construct a tuple of any length at runtime, then tuples are like slices. In
any case, Go already has all three work-alikes, and it seems it'd break the
orthogonality thrice-ways if "tuples" were added.

I don't see this "destructuring assignment" couldn't be done with slices,
as something of the inverse of how variadics are handled. You extend `...`
to be allowed in arbitrary single-valued expressions who's type happens to
be a slice. In this case, line 14 of the original playground snippet would
be changed to:

var s1, s2, s3 = GetNStrings(3)...

Behavior:

1) if the returned slice, at runtime, has less than the desired number of
elements, the remaining variables get a zero value.
2) if the returned slice, at runtime, has less than the desired number of
elements, panic.

In the case of there being more elements than desired, we can be more
flexibile:
1) if there's a trailing blank variable, extra elements are ignored. ex:

var s1, s2, s3, _ = GetNStrings(3)...

2) if there's no trailing blank, and there are extra elements, panic.

3) if there's a leading blank, it absorbs zero or more elements at the
beginning of the slice, with variables being assigned in forward order on
the tail of the slice.

The compiler would detect expressions like the above, and do size checking
as needed. When not used in an assignment, intermediates could be used,
such as when filling a slice into a non-variadic (currently, slices can
only be filled into variadics, of course).

Anyway, since some behavior would necessarily have to be pushed into the
runtime logic (though it's certainly doable with slices), either adding
more spots for panics to occur, or for data to get "lost" (and thus
comparatively impossible to debug), despite my believing it's doable, I
don't really see the great benefit it would bring. Trying to pound a
variable-length peg into a fixed-length hole _feels_ like it'll be the
cause of many bugs, and will be abused by being used when the existing
multi-value aspects of the language would have been an all-around cleaner
solution.
On Thursday, November 1, 2012 1:52:13 AM UTC-6, minux wrote:


On Thu, Nov 1, 2012 at 3:42 PM, Job van der Zwan <j.l.van...@gmail.com<javascript:>
wrote:
On Thursday, 1 November 2012 08:00:08 UTC+1, Stephen Day wrote:

He's actually asking for destructing assignment, which is unsupported in
Go.

Do you mean destructuring assignment? I just googled "destructing
assignment" and that's what it returned instead. In either case, I had
never heard of the term before (so thanks for introducing me to it), but it
looks to me that even if Go did support destructuring assigment, it
shouldn't support it in this case: there's no way for the compiler to know
what the length of the returned slice would be, so there's no way to guess
if assigning to three variables is legal or not. Unless it would something
like "just assign the first three values of the slice, and if the slice is
smaller assign zero", which sounds like a horrible idea to me.
right. if we had a tuple type, this destructuring assignment would be
possible.
--

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 10 of 22 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 31, '12 at 9:15a
activeNov 2, '12 at 7:51a
posts22
users11
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase