x and the evaluation
of y is not specified".At least for me this seems very confusing.
The writing doesn't seem to explain what the cause is. Just a reference "because the evaluation order of this sort of expression is not specified".
fn := func() int { x = 2; return 0 }<me> the following expression:
x + // 1
fn() // 0, but x=2
// evaluating to 2
x + // 1
fn() + // 0, but x=2
x // 2
// evaluating to 3
fn() + // 0, but x=2
x + // 2
x // 2
// evaluating to 4 <me> and it's not possible to change the evaluation order by using parentheses
--
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...@googlegroups.com.
To my understanding the memory model must also specify the happens-before relationship within one goroutine. In this case it specifies it to be the program order. Which brings me to my mistake in bringing this up, as it’s not the memory model’s fault that the evaluation order is what is is, but the evaluations happen in some order, which then must satisfy the happens-before requirements of the memory model.
Bottom line: I mistakenly though memory model had something to do with this, whereas the problem arises solely from the unspecified order of evaluation.
Yet I am still confused
by the fact that I cannot change the evaluation order by using
parentheses. Of the three possible evaluation orders, the go
compiler chooses to use “evaluate function first”, then the rest
(fn() + x + x =
4), but shouldn’t it be possible to force the evaluation order
by saying (x + fn()) + x =
3?
Test: https://play.golang.org/p/fgIJlI5nup
Sorry if I'm totally in
the woods with this :(
-Gima
I apologize, I really do. I must be becoming a bother by now. I just Cannot go to sleep without getting this off my chest.
I can understand that the evaluation order doesn’t constrain function and variable evaluation in an expression, but I cannot understand how the following can be allowed and correct behavior:
https://play.golang.org/p/ZDaNFYS4Rm
The outcome is 3 with C,
Javascript and Java, whereas go compiler says it’s 4 (which
clearly indicates that fn() was
called twice before evaluating either of the parens, which with
the parentheses present, seems outright wrong.
So is this indeed
correct behavior? If so, I’ll just be done with it.
--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/HQ59BBbhCT0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
The outcome is 3 with C, Javascript and Java, whereas go compiler says it’s 4