simplify map ins/del syntax?

78 views
Skip to first unread message

Mark Summerfield

unread,
Jun 3, 2011, 4:53:17 AM6/3/11
to golang-nuts
Hi,

At the moment the map ins/del syntaxes are:

m[k] = v // ins
m[k] = v, true // ins
m[k] = v, false // del

One proposal has been to make del:

m[k] = _

But it occurs to me that if that were done the whole syntax could be
collapsed to:

m[k] = v // ins
m[k] = _ // del

Which avoids the need to use a value when deleting.

--
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
C++, Python, Qt, PyQt - training and consultancy
"Programming in Python 3" - ISBN 0321680561
http://www.qtrac.eu/py3book.html

David Symonds

unread,
Jun 3, 2011, 5:22:03 AM6/3/11
to Mark Summerfield, golang-nuts
I like this. I was pondering exactly that syntax earlier in the week,
but hadn't had time to write it up.

The existing way has the cute ability to make the insertion/deletion
choice at runtime, but I don't think I've seen a compelling place
where that's been used where a simple if block wouldn't be bearable.


Dave.

chris dollin

unread,
Jun 3, 2011, 5:34:22 AM6/3/11
to Mark Summerfield, golang-nuts
On 3 June 2011 09:53, Mark Summerfield <li...@qtrac.plus.com> wrote:
> Hi,
>
> At the moment the map ins/del syntaxes are:
>
>    m[k] = v        // ins
>    m[k] = v, true  // ins
>    m[k] = v, false // del
>
> One proposal has been to make del:
>
>    m[k] = _
>
> But it occurs to me that if that were done the whole syntax could be
> collapsed to:
>
>    m[k] = v // ins
>    m[k] = _ // del

It's a nice syntactic trick but it does not make me happy,
having insert vs delete be indicated by a pretend value
(ie _, which serves this purpose nowhere else).

I'd prefer

m[k] = ()

which at least has a notion of "nothing here guv", but
is rather too clever for my taste and uncomfortably
fragile.

A horrible twofold overloading of -- would allow

m -- k

but I wouldn't dare suggest

m ++ k

as the (an) idiom for putting things in maps.

Chris

--
Chris "allusive" Dollin

Russ Cox

unread,
Jun 3, 2011, 11:24:38 AM6/3/11
to David Symonds, Mark Summerfield, golang-nuts

Sol Toure

unread,
Jun 3, 2011, 11:30:17 AM6/3/11
to chris dollin, Mark Summerfield, golang-nuts
+1 for built-int "del"
del(m[k])

Jan Mercl

unread,
Jun 3, 2011, 11:37:11 AM6/3/11
to golan...@googlegroups.com
+1 for keeping the map operations syntax as they are now OR *add* to the current syntax the shortcut "m[k] = _".

Miguel Pignatelli

unread,
Jun 3, 2011, 11:41:35 AM6/3/11
to golan...@googlegroups.com
On 03/06/11 16:37, Jan Mercl wrote:
> +1 for keeping the map operations syntax as they are now OR *add* to the
> current syntax the shortcut "m[k] = _".

Agree,

-1 for a new built-in (del, rem, remove, etc...)

M;

chris dollin

unread,
Jun 3, 2011, 12:06:39 PM6/3/11
to Sol Toure, Mark Summerfield, golang-nuts
On 3 June 2011 16:30, Sol Toure <sol...@gmail.com> wrote:
> +1 for built-int "del"
> del(m[k])

I think this is a really bad idea, because it introduces something
new into Go's semantics.

Go functions are given values. How those values are built is not
of interest to the function. This is true even for the magic functions
`make` and `new` which have Go types as arguments.

For del(m[k]) to work, del has to look at the structure of the
argument /expression/, not at its /value/, because the
run-time operation needs two things: a map m and a key k,
separately.

While the language and compiler are of course to pick any tango
they like to dance, having a single isolated place in the language
where something that isn't a type has structure that must be
respected by a builtin does not strike me as a good idea.

del(m,k) would be miles better; I'm not saying I like it, but
it would be better.

Reply all
Reply to author
Forward
0 new messages