map syntax for deletion doesn't permit the blank identifier

16 views
Skip to first unread message

Kelvin Kakugawa

unread,
Nov 12, 2009, 9:59:07 AM11/12/09
to golang-nuts
I noticed that the map syntax for membership permits the use of the
blank identifier:
_, present := timeZone[tz];

However, the converse isn't true. If I want to delete an entry from
the map, this doesn't work:
m := make(map[int]string);
m[2] = "c";
m[2] = _, false;

The compiler errors out like so:
test.go:13: cannot use _ as value

I have to specify an emptry string:
m[2] = "", false;

I was wondering if it would be reasonable to allow the blank
identifier to be used, when deleting a key from a map?

-Kelvin

Dimiter "malkia" Stanev

unread,
Nov 12, 2009, 10:46:38 AM11/12/09
to golang-nuts
I think this would be problematic:

Consider:

m[2] = _, someVarThatIsTrueOrFalse

if someVarThatIsTrueOrFalse happens to be true, then what should be
put in the map?

Stephan Z.

unread,
Nov 12, 2009, 11:10:57 AM11/12/09
to golang-nuts
I guess you should use nil for this.

- Stephan

Kelvin Kakugawa

unread,
Nov 12, 2009, 11:18:41 AM11/12/09
to Stephan Z., golang-nuts
I think the above points are relevant as to the purpose of the blank identifier.

I did try nil, as well:
test.go:27: cannot use nil as type string

I suppose a del(map[K]) function, like Python's, might be interesting.
However, I don't know whether the language designers would think it
fits.

-Kelvin

Marcin 'Qrczak' Kowalczyk

unread,
Nov 12, 2009, 11:49:17 AM11/12/09
to Kelvin Kakugawa, golang-nuts
2009/11/12 Kelvin Kakugawa <kaku...@gmail.com>:

> I have to specify an emptry string:
> m[2] = "", false;
>
> I was wondering if it would be reasonable to allow the blank
> identifier to be used, when deleting a key from a map?

The syntax of deletion is just stupid. Why would a user need to
provide a value if he wants to delete the entry and this value will be
ignored anyway?

--
Marcin Kowalczyk

Jason

unread,
Nov 12, 2009, 12:20:10 PM11/12/09
to golang-nuts
I like your style, Mr. Kowalczyk :)

I agree, too. Deletion should not require a value. I like tuples as
much as the next guy... I just don't think this makes sense. Even that
the line of code could do an insert or a delete depending on the value
of a boolean variable could make code completely unreadable.

-J

On Nov 12, 8:49 am, "Marcin 'Qrczak' Kowalczyk" <qrcza...@gmail.com>
wrote:
> 2009/11/12 Kelvin Kakugawa <kakug...@gmail.com>:

Antoine Chavasse

unread,
Nov 12, 2009, 12:26:52 PM11/12/09
to Jason, golang-nuts
On Thu, Nov 12, 2009 at 6:20 PM, Jason <jki...@gmail.com> wrote:
I like your style, Mr. Kowalczyk :)

I agree, too. Deletion should not require a value. I like tuples as
much as the next guy... I just don't think this makes sense. Even that
the line of code could do an insert or a delete depending on the value
of a boolean variable could make code completely unreadable.

I think they did it for the sake of symmetry with looking up a map entry ( value, ok = map[key] ) but it doesn't really seem necessary.

map[key] = nil would imo be cleaner and more intuitive to erase a key.

Dimiter "malkia" Stanev

unread,
Nov 12, 2009, 2:30:46 PM11/12/09
to golang-nuts
What if you want your key value to be nil.

E.g. the key is present, but with nil value?

I also think that map[key] = _, false is a bit stupid, but maybe it's
due to some generalizaation (reminds a bit of how setf is done in
Common Lisp)

On Nov 12, 9:26 am, Antoine Chavasse <a.chava...@gmail.com> wrote:

Antoine Chavasse

unread,
Nov 12, 2009, 2:36:00 PM11/12/09
to Dimiter malkia Stanev, golang-nuts
On Thu, Nov 12, 2009 at 8:30 PM, Dimiter "malkia" Stanev <mal...@gmail.com> wrote:
What if you want your key value to be nil.

E.g. the key is present, but with nil value?


It's a matter of whether this is a common enough need to justify the awkward key deletion syntax. I know that some languages like lua have eschewed this ability and aren't the worse for it (incidentally in lua it's map[key] = nil just like I suggested)
Reply all
Reply to author
Forward
0 new messages