Skip to content

proposal: spec: allow having default values for function declaration - Go2 #50766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mostafatalebi opened this issue Jan 23, 2022 · 1 comment

Comments

@mostafatalebi
Copy link

mostafatalebi commented Jan 23, 2022

As of now (go <= 1.17) every function definition needs to declared function parameters as required. We cannot have any default value assigned to the function, for achieving this, the developer needs to adds conditions inside the function, and the user-caller of the function, if not interested in the param, must pass nil as value, and if the parameter is not a pointer type, he is required to pass a zero-literal of that specific type. All of this can be handled by introducing a new feature in go2 that supports specifying a default value in function declaration. The benefits of this:

  • Less code for sure, as many conditions inside the beginning of the functions would be eliminated, as they are not necessary anymore
  • Compatible with existing code-bases, as it introduces no breaking changes to the current code-bases of go1.*
  • More readable codes, less obligatory type-specific zero-values for function invocations
  • More welcoming to new developers landing form other languages, as most of them support this feature

Regarding the compile cost, others might have some points.

@gopherbot gopherbot added this to the Proposal milestone Jan 23, 2022
@mostafatalebi mostafatalebi changed the title proposal: spec: allow having default values for function declaration proposal: spec: allow having default values for function declaration - Go2 Jan 23, 2022
@robpike
Copy link
Contributor

robpike commented Jan 23, 2022

Duplicate of #21909

@robpike robpike marked this as a duplicate of #21909 Jan 23, 2022
@golang golang locked and limited conversation to collaborators Jan 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants