On Thu, Jun 1, 2017 at 9:52 PM, Will Hawkins <hawki...@borlaugic.com> wrote:
> On Fri, Jun 2, 2017 at 12:41 AM, Ian Lance Taylor <i...@golang.org> wrote:
>>
>> Unlike function calls, the language spec doesn't anything about when a
>> dereference operation is executed.  And as you can see the current
>> compiler does not do the dereference immediately after the function
>> call.
>
> This is the bit of information that I was hoping you'd have. It seems
> like an odd omission from the spec. On one hand, it clearly allows the
> compiler to perform optimizations that otherwise might not be
> possible. On the other, though, it would seem to introduce the
> possibility of data races or, as in the case here, unexpected results.
> Just my naive opinion. If you had thoughts about it, I'd love to hear
> your expert opinion.

I would say that technically it does not introduce the possibility of
data races.  The only way to avoid a data race in Go is to use a
channel operation or an explicit lock, such as a sync.Mutex, which
requires a function call.  There is a specified order of evaluation
for both channel operations and function calls, so there is no
possible confusion about whether there is a data race there.  If your
program can have a data race due to two goroutines doing a pointer
indirection simultaneously, then your program has a data race
regardless of the order of execution.

It is true that you can have unexpected results, but only if you are
using a style of programming that is rather prone to
misunderstandings.  So it seems hard to get too worked up by it.

Anyhow, to answer your real question, at various times we did discuss
fully specifying the order of evaluation of all operations.  But we've
never decided to do it for the reason you mention: it puts significant
constraints on the compiler.  That, combined with the fact that the
kinds of programs where the order of evaluation makes a difference are
programs that are already confusing, makes it seem not worth
specifying in the language.


> I sincerely hope that what I asked was not a stupid question. It gave
> me lots of trouble and I worked through the problem with several
> others and we could not figure out the cause of the discrepancy.

No worries, it is not at all a stupid question.

Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to