variables - Why does Go allow compilation of unused function parameters? -


one of more notable aspects of go when coming c compiler not build program if there unused variable declared inside of it. why, then, program building if there unused parameter declared in function?

func main() {     print(computron(3, -3)); }   func computron(param_a int, param_b int) int {     return 3 * param_a; } 

there's no official reason, reason given on golang-nuts is:

unused variables programming error, whereas common write function doesn't use of arguments.

one leave arguments unnamed (using _), might confuse functions like

func foo(_ string, _ int) // what's supposed do?

the names, if they're unused, provide important documentation.

andrew

https://groups.google.com/forum/#!topic/golang-nuts/q09h61oxwww

sometimes having unused parameters important satisfying interfaces, 1 example might function operates on weighted graph. if want implement graph uniform cost across edges, it's useless consider nodes:

func (graph *mygraph) distance(node1,node2 node) int {     return 1 } 

as thread notes, there valid argument allow parameters named _ if they're unused (e.g. distance(_,_ node)), @ point it's late due go 1 future-compatibility guarantee. also mentioned, possible objection anyway parameters, if unused, can implicitly provide documentation.

in short: there's no concrete, specific answer, other made arbitrary (but still educated) determination unused parameters more important , useful unused local variables , imports. if there once strong design reason, it's not documented anywhere.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -