3 ms·
The final return does add information. It is common to name return variables to make the meaning clear to prospective users, without using them in the body. Jus
by rsc 13y ago
The final return does add information. It is common to name return variables to make the meaning clear to prospective users, without using them in the body. Just to pick the first one I found, here is the definition for regexp.Match:
// Match checks whether a textual regular expression
// matches a byte slice. More complicated queries need
// to use Compile and the full Regexp interface.
func Match(pattern string, b []byte) (matched bool, error error) {
re, err := Compile(pattern)
if err != nil {
return false, err
}
return re.Match(b), nil
}
If the final line is missing, the compiler rejects the function. You might well have meant 'return', which could be inserted implicitly, but you might also have been interrupted while writing the function and meant to write a 'return something'. The compiler (really the language) requires you to be clear to avoid inferring an incorrect completion.
One nit: the return rules are separate from escape analysis, which is about keeping things on the stack instead of allocating them on the heap.
- taliesinb 13y ago1. Or maybe you were interrupted right after writing "return" :-). This one weird simple trick could work (and give you white teeth): implicit returns only get inserted if all the named return variables are assigned-to at least once in the body of the function. Perhaps that breaks your pure syntactic rule, though. 2. Yup, wrong term.