-
Notifications
You must be signed in to change notification settings - Fork 18.3k
Closed
Labels
FrozenDueToAgeLanguageChangeSuggested changes to the Go languageSuggested changes to the Go languageProposalv2An incompatible library changeAn incompatible library change
Milestone
Description
Languages like Python and TypeScript supply ways to omit arguments when calling with either a default provided, or just none at all.
Default parameter example from Typescript:
function buildFileName(directory: string, fileName: string, delim = "/"): string {
return `${directory}${delim}${fileName}`;
}
let res = buildFileName("/root", "test.txt"); // valid, results in /root/test.txt
let res = buildFileName("c:", "test.txt", "\\"); // valid, results in c:\test.txt
Optional parameter example from Typescript:
function doMath(x: number, y?: number): number {
if (!y) return x;
return x + y;
}
let res = doMath(1); // valid, results in 1
let res = doMath(1, 2); // valid, results in 3
Use Case
For instance, creating a reader:
func NewReader(rd io.Reader) {}
func NewReaderSize(rd io.Reader, size int) *Reader {}
This can be compressed into one function:
func NewReader(rd io.Reader, size = 4096) *Reader { /* ... */ }
// This should work too:
func NewReader(rd io.Reader, size = defaultBufferSize) *Reader { /* ... */ }
2Cubed, OneOfOne, mrd0ll4r, rajathagasthya, urandom and 12 morecznic, jimmyfrasche, dsnet, mpx, mvdan and 10 more
Metadata
Metadata
Assignees
Labels
FrozenDueToAgeLanguageChangeSuggested changes to the Go languageSuggested changes to the Go languageProposalv2An incompatible library changeAn incompatible library change