many conventional operators become delimiters in Go - am I right?

8 views
Skip to first unread message

Ganesh

unread,
Dec 24, 2009, 4:38:57 AM12/24/09
to golang-nuts
Hi,

(I am a newbie, so forgive me if its a silly question).

I was impressed with the very small operator precedence table in Go. I
just checked the C precendence table and operators and tried comparing
it.

I notice that there are still operators like ++, = (and compound
assignment), . (member selection), (cast), () function call, etc are
still there implicitly. Yes, some operators are removed like ->, ,
(comma), ?: etc. But new operators are added, like <-, &^ etc. So, the
operators are still there, but in the name of delimiters and treated
in grammar, making the operator precedence look small. In effect, the
precedence is still there. Like ++ is not an operator, but is made as
part of IncDecStmt, so effectively its an operator, (though listed as
a delimiter) with implicit precedence. Am I right?

-ganesh

Peter Froehlich

unread,
Dec 24, 2009, 9:21:19 AM12/24/09
to Ganesh, golang-nuts
Hi all,

On Thu, Dec 24, 2009 at 4:38 AM, Ganesh <sgga...@gmail.com> wrote:
> precedence is still there. Like ++ is not an operator, but is made as
> part of IncDecStmt, so effectively its an operator, (though listed as
> a delimiter) with implicit precedence. Am I right?

The idea of precedence applies inside expressions. IncDecStmt is an
instruction (or a statement to use the common misnomer). Even if used
in a "for" loop, i++ doesn't produce a value, so you can't write
anything like (in C)

i++ + ++i

where you'd get into precedence issues. (Note that in C there are
additional concerns with i++ namely sequence points; so the above is a
really bad idea regardless of precedence.)

Cheers,
Peter
--
Peter H. Froehlich <http://www.cs.jhu.edu/~phf/>
Senior Lecturer | Director, Johns Hopkins Gaming Lab

Ian Lance Taylor

unread,
Dec 28, 2009, 12:47:39 PM12/28/09
to Ganesh, golang-nuts
Ganesh <sgga...@gmail.com> writes:

++ and = are not operators in the normal sense. They can only be used
once in a statement. Likewise for compound assignment.

Selectors, function calls, and type assertions can be viewed as
operators but the grammar specifies when they may be used. In effect
they are all at equal precedence, left associative, and the precedence
is higher than any other operator. You didn't mention [] which is in
the same class.

There are also prefix unary operators (+, -, !, etc.). They can be
viewed as forming a precedence class in between those operators and
the other operators.

Ian

Reply all
Reply to author
Forward
0 new messages