_ as value when deleting from a map

210 views
Skip to first unread message

Keith Rarick

unread,
Aug 18, 2010, 8:15:54 PM8/18/10
to golan...@googlegroups.com
It would be nice to write

m[k] = _, false

to delete from a map. In this case, _ could be defined as a zero value
of the appropriate type, though as an optimization the compiler could
just delete the item without bothering to construct the value.

kr

Rob 'Commander' Pike

unread,
Aug 18, 2010, 8:28:39 PM8/18/10
to Keith Rarick, golan...@googlegroups.com
Perhaps, but that only works if the boolean is a constant. If it's a
variable, there needs to be an actual value. You're right that the
meaning could be 'the zero value', although that's a new meaning of
the blank identifier.

Interesting idea, though.

-rob

Rob 'Commander' Pike

unread,
Aug 18, 2010, 8:30:52 PM8/18/10
to Keith Rarick, golan...@googlegroups.com
If we're going crazy (which we're not) we could reduce it to

map[k] = _

That's yet another meaning of _, of course. It's cute, too cute.

Map deletion is important but rare enough that it doesn't need
fine-tuning syntactically.

-rob

Krzysztof Kowalczyk

unread,
Aug 18, 2010, 9:44:35 PM8/18/10
to Rob 'Commander' Pike, Keith Rarick, golan...@googlegroups.com
Have you considered:

del map[k]

like in Python?

The intent of del map[k] is clearer than map[k] = _, false, for
beginners and experts alike.

I would argue that it would help readability because overloading
assignment for delete is confusing and that icky feeling doesn't go
away even if you know the idiom.

-- kjk

Russ Cox

unread,
Aug 18, 2010, 10:03:58 PM8/18/10
to Krzysztof Kowalczyk, golan...@googlegroups.com
> The intent of del map[k] is clearer than map[k] = _, false, for
> beginners and experts alike.

If you know about x, ok = m[k] and what it means for ok to be false,
then m[k] = x, false should be plenty clear. If you don't know
about that, well you should, beginner and expert alike.

Russ

Popog

unread,
Aug 18, 2010, 10:53:05 PM8/18/10
to golang-nuts
On Aug 18, 5:30 pm, "Rob 'Commander' Pike" <r...@google.com> wrote:
> If we're going crazy (which we're not) we could reduce it to
>
>     map[k] = _
>
> That's yet another meaning of _, of course. It's cute, too cute.

I actually like this to a degree. Maybe even have "c <- _" close a
channel. Reading from "/dev/null/' (which is what I associate _ with)
is an immediate EOF, so it makes sense from that perspective.

Otherwise I agree with "delete(map[k])" or "remove(map[k])". It makes
much more sense and keeps the language more orthogonal. If you want
conditional removal, use an if statement.

Jessta

unread,
Aug 18, 2010, 11:05:16 PM8/18/10
to Popog, golang-nuts

That would have to make delete() or remove() built-ins which would
make it impossible to have a delete() and remove() function as part of
your package, there are already enough built-ins.

I like map[k] = _ but it would be confusing because you couldn't
assign '_' to anything else.
The current version makes much more sense.

if x, ok = m[k] then semantically m[k] is representing two values of
type T and bool.
it's similar to
x,ok = OfTypeT,true

so mk[k]=x,false is similar to:
T,bool = x,false

- jessta


--
=====================
http://jessta.id.au

Scott Lawrence

unread,
Aug 19, 2010, 3:32:02 AM8/19/10
to Popog, golang-nuts
On 8/18/10, Popog <pop...@gmail.com> wrote:
> I actually like this to a degree. Maybe even have "c <- _" close a
> channel. Reading from "/dev/null/' (which is what I associate _ with)
> is an immediate EOF, so it makes sense from that perspective.
I'm not sure if I like this (yet), but as long as we're finding uses
for _, I'd like to see a null type. struct{} is too hard to type when
I don't intent to use the data.

type T struct{}
func (T) SomeMethod() (s string, e os.Error) { /* stuff */ }

(Tell me if I missed the good way to do that.)

--
Scott Lawrence

Marcin 'Qrczak' Kowalczyk

unread,
Aug 19, 2010, 3:58:27 AM8/19/10
to r...@golang.org, Krzysztof Kowalczyk, golan...@googlegroups.com
2010/8/19 Russ Cox <r...@golang.org>:

'm[k] = x, false' forces the programmer to provide a value for the
map, while deleting a map element conceptually does not depend on any
value for the map. Forcing to provide an argument which is known to be
ignored is silly.

--
Marcin Kowalczyk

Krzysztof Kowalczyk

unread,
Aug 19, 2010, 4:59:53 AM8/19/10
to Marcin 'Qrczak' Kowalczyk, r...@golang.org, golan...@googlegroups.com
I'll +1 Marcin and expand a bit on my thinking.

I like x, ok = m[k]. Even though it's not an idiom used in other
languages, it has good properties:
1. it's pragmatic i.e. it makes code shorter and it elegantly
addresses an issue I often bump into when working with dicts in e.g.
python where I have to split my code into checking if element exists
in a dict and accessing it. if's abound in that case.
2. it's still an assignment. Once I know about multi-value return, I
have no problem understanding what that particular multi-value
assignment does. No surprises here.

m[k] = _, false, on the other hand:
1. Is not pragmatic. It forces me to provide a value (_) that is
ignored. It's intent is more obscure than e.g. del m[k] and it's not
shorter than del m[k]
2. it overloads the meaning of assignment to be delete. It doesn't
really make sense that a syntax that in every other context means
"assign" in this particular context means "delete". It is a surprise.
Mental context switch is needed to parse it differently in that
context.

The only (dubious) thing it has going for it is to preserve symmetry of:
x, ok = m[k] <=> m[k] = _, false
but to me it's a superficial similarity and not worth sacrificing
points 1) and 2).

Even m[k] = _ would be better because I could convince myself a little
more that assigning a non-existent value is like delete (and it is
shorter). I guess what bothers me most in that syntax is that it does
look like an assignment hence my primary way of interpreting it is as
an assignment and from that perspective it doesn't make sense.

I'm not convinced by "you should just learn how it works" argument
(I'm paraphrasing, maybe unfairly). I know what that syntax does
because I've read the tutorial and I've written some code. It's not
that I can't learn it or use it, it's that after I learned it and used
it, it doesn't make conceptual sense to me and I believe del m[k] (or
m[k] = _) are better from pragmatic and conceptual point of view.

-- kjk

jimmy frasche

unread,
Aug 19, 2010, 4:05:16 PM8/19/10
to Krzysztof Kowalczyk, Marcin 'Qrczak' Kowalczyk, r...@golang.org, golan...@googlegroups.com
I personally like being able to do things like

m[k] = x, x > 1

Reply all
Reply to author
Forward
0 new messages