Earlier quoted context omitted.
Not my area of expertise and it is yours, but if you eliminate a zero instruction before a copy instruction how can you be sure that doesn't affect other threads? var x Int // Pass x to a thread by reference x = 0 time.Sleep(1000) x = 1
How can you be sure that the other threads ever see it in time? they might be suspended for a whole second because a HDD needs to spin up or something like that. So threads never seeing the value is already a valid outcome, so the compiler might as well always do that.
Benefits of named return values in Go
91–100 of 146 posts
Re: Benefits of named return values in Go
#92Earlier quoted context omitted.
Not my area of expertise and it is yours, but if you eliminate a zero instruction before a copy instruction how can you be sure that doesn't affect other threads? var x Int // Pass x to a thread by reference x = 0 time.Sleep(1000) x = 1
The answer to that one would be to embed thread-safety in the type system, aka. Rust. For languages with less sophisticated type systems you get a choice between inefficiency (Go), or complicated rules which state that the programmer is wrong for coding that way (C).
In general I don't think Rust actually adds much abstraction that isn't already in say Python. What it does is enforce tight constraints.
Re: Benefits of named return values in Go
#93This clouds up your godoc with unnecessary names though. Why not just initialize a default value at the top of your function? You don't get the bare returns but it's a lot cleaner.
Re: Benefits of named return values in Go
#94Earlier quoted context omitted.
The answer to that one would be to embed thread-safety in the type system, aka. Rust. For languages with less sophisticated type systems you get a choice between inefficiency (Go), or complicated rules which state that the programmer is wrong for coding that way (C).
Your "solution" is possible in many languages. It's just to give threads complete ownership of data they use. It doesn't require a special type system. In general I don't think Rust actually adds much abstraction that isn't already in say Python. What it does is enforce tight constraints.
Re: Benefits of named return values in Go
#95This clouds up your godoc with unnecessary names though. Why not just initialize a default value at the top of your function? You don't get the bare returns but it's a lot cleaner.
In my perfect world, we would have named return values by default, so then we could use them to document the return values, but never empty returns. Empty returns are the only problem with named return values Imo, as it's easy to accidentally slip values into the return that you didn't intend.
Though, I had always wondered about returning copies like Foo{} on every return.. guess I know now.
edit: As an aside, I should not dismiss your cluttering comment. I did so, because I feel it's unrelated to this exact topic. If named return values, something which by it's very nature conveys additional information, is cluttering the godocs, then that should be a bug, and filed/fixed accordingly. Imo.
Re: Benefits of named return values in Go
#96Earlier quoted context omitted.
I'm pretty sure Go does compile down to an IR, but it's little more than an abstraction over different architectures. I could be wrong.
Originally it compiled to Plan9 assembly which is cross platform. They have an SSA backend for some architectures now.
Re: Benefits of named return values in Go
#97Earlier quoted context omitted.
Your "solution" is possible in many languages. It's just to give threads complete ownership of data they use. It doesn't require a special type system. In general I don't think Rust actually adds much abstraction that isn't already in say Python. What it does is enforce tight constraints.
The constraints are important. They're what let you code safely, and also give the compiler the ability to optimize better.
Re: Benefits of named return values in Go
#98Earlier quoted context omitted.
For the most part named returns are frowned upon, and while it is admittedly an oddity in the language, having one oddity is still achieving the stated goal of "few surprises". :)
Are they actually frowned upon? I've been writing Go for a few years now and while I acknowledge there will be blind spots in my knowledge, this is still the first I've heard against using that language feature. In fact named returns appear all over the Go source code itself[0] and the Effective Go section on named returns[1] seems positive about their usage. [0] https://github.com/golang/go/blob/master/src/os/file.g…
/r/golang consensus is against using named values. I disagree with it, but the consensus is there. Or at least I think that at the very least named return values should indicate that someone is doing something with a defer.
As for why, I don't get a clear sense. I hate to characterize an argument I disagree with, but I have to admit I just sort of get the sense that it was one of those things that just sorta happened. Sometimes one person says something, another person half-heartedly agrees, and before you know it it's getting parroted around by an entire community. I acknowledge again that as I don't agree with it this may be a rather harsh read on my part.
Re: Benefits of named return values in Go
#99Earlier quoted context omitted.
I'm thinking of it in the way that the interpreter works in JavaScript with tasks. If you setTimeout(A, 0), inside B, then after B returns, A is called, but any other tasks previously inserted into the queue are called first. So I think that means I was asking about execution in the same context (as in memory context), unless you mean stack frame by context, in which case, I think i understand that because the caller…
It's much simpler than that. It's just a way of defining cleanup functions without having a language-level concept of destructors. Here's an example: https://gobyexample.com/defer . This simple example should explain everything: package main import "fmt" func function1() { defer fmt.Println("function1: defer a") fmt.Println("function1: inside") defer fmt.Println("function1: defer b") } func main() { fmt.Println("main…
Re: Benefits of named return values in Go
#100Earlier quoted context omitted.
Are they actually frowned upon? I've been writing Go for a few years now and while I acknowledge there will be blind spots in my knowledge, this is still the first I've heard against using that language feature. In fact named returns appear all over the Go source code itself[0] and the Effective Go section on named returns[1] seems positive about their usage. [0] https://github.com/golang/go/blob/master/src/os/file.g…
"Are they actually frowned upon? I've been writing Go for a few years now and while I acknowledge there will be blind spots in my knowledge, this is still the first I've heard against using that language feature." /r/golang consensus is against using named values. I disagree with it, but the consensus is there. Or at least I think that at the very least named return values should indicate that someone is doing someth…
The way return works is dictated by the existence of a perhaps bad name in the start of the function declaration, in a place most likely to fall off screen in a split editor scenario.
I called it sneaky because I think it's hard to see. I don't get the value of this technique over, say, naming and returning locals. Certainly from a performance perspective it will be identical.