It’s very C++ like - good to see the specter is still alive. Isn’t UB great?

On Jul 17, 2026, at 11:17 PM, [email protected] <[email protected]> wrote:


Hi, thanks for the explanations.

I think the example in the doc:

a := 1
f := func() int { a++; return a }
x := []int{a, f()} // x may be [1, 2] or [2, 2]: evaluation order between a and f() is not specified

is exactly why I'm so confused. Maybe the evaluation order of function calls and values should be specified under a single rule somehow? Before this becomes a widely spread most-ask job interview question, perhaps?
On Friday, July 17, 2026 at 11:57:10 PM UTC+8 Marvin Renich wrote:
* [email protected] <[email protected]> [260717 09:45]:
> Hi all,
>
> So, given following code:
[snip]
> func TestNumAdd(t *testing.T) {
> n := Num(10)
> a, b, c := n, n.add(1), n.add(2)
> // a, b, c := n.get(), n.add(1).get(), n.add(2).get()
> fmt.Println(a, b, c)
> }
>
> What do you think the `TestNumAdd` function will output?

The Go Language Specification is very explicit about this:

https://go.dev/ref/spec#Order_of_evaluation

See the explanation under the first example:

> However, the order of those events compared to the evaluation and
> indexing of x and the evaluation of y and z is not specified,
> except as required lexically.

So, all function calls (n.add()) happen in left-to-right order, but when
they are evaluated relative to the evaluation of simple variables (n in
this case) is explicitly unspecified.

> PS. I also asked Gemini the same question above, and it was also confused.

What did you expect from an LLM? It only knows what other people have
told it, and even then it has a hard time telling when it has been told
the truth or a falsehood.

...Marvin

--
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 [email protected].
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/1859ffde-cc7e-4f78-81db-9be5abfc3197n%40googlegroups.com.

--
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 [email protected].
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/A988E71E-BCF3-4080-A36D-808A6D92C771%40me.com.

Reply via email to