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
proposal: Go 2: support statically typed kwargs as an alternative to option structs #39182
Comments
Something like this already exists, but it might need to be slightly changed. I propose this: func NewHouse(opts struct{Material string, HasFireplace bool, Composable bool, Floors int}) *House The usage for that would be: NewHouse(struct{
Material: "stuff",
}) Worth pointing out that this is similar to Zig's anonymous struct literals API: dump(.{
.int = @as(u32, 1234),
.float = @as(f64, 12.34),
.b = true,
.s = "hi",
}) |
Possible dup of #12854 |
You can already do this, but the syntax for calling the function is not so nice: https://play.golang.org/p/LzJxchVbWJn package main import ( func Foo(p struct { I int ; S string ; }) { func main() { |
This comment has been minimized.
This comment has been minimized.
As discussed above, this seems similar to other proposals involving structs and composite literals. In particular, type inference on structs would seem to permit this, while also permitting other things as well. Emoji voting for this proposal is not in favor. For these reasons, this particular proposal is a likely decline. Leaving open for four weeks for final comments. -- for @golang/proposal-review |
No further comments. |
Option structs have become a de-facto standard mechanism of handling complex parameters for functions, it would be nice to be able to write statically typed key words arguments as an alternative to this binding structs-per-function. This would help de-clutter large code-bases where at least local conventions have been writing...
Instead we could have
Saving six lines (even if the compiler compiles a Struct out of it).
The text was updated successfully, but these errors were encountered: