Skip to content
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

Closed
caldempsey opened this issue May 20, 2020 · 6 comments
Labels
Milestone

Comments

@caldempsey
Copy link

caldempsey commented May 20, 2020

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...

type HouseOptions struct {
	Material     string
	HasFireplace bool
        Composable bool
	Floors       int
}
func NewHouse(opts HouseOptions) *House {
// blah
}

Instead we could have

func NewHouse(opts: {Material: string, HasFireplace: bool, Composable: bool, Floors: int}) *House {
// blah
}

Saving six lines (even if the compiler compiles a Struct out of it).

@gopherbot gopherbot added this to the Proposal milestone May 20, 2020
@caldempsey caldempsey changed the title proposal: support kwargs as an alternative to option structs proposal: go2 support kwargs as an alternative to option structs May 20, 2020
@caldempsey caldempsey changed the title proposal: go2 support kwargs as an alternative to option structs proposal: go-2 support kwargs as an alternative to option structs May 20, 2020
@caldempsey caldempsey changed the title proposal: go-2 support kwargs as an alternative to option structs proposal: go-2 support statically typed kwargs as an alternative to option structs May 20, 2020
@diamondburned
Copy link

diamondburned commented May 20, 2020

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",
})

@jimmyfrasche
Copy link
Member

Possible dup of #12854

@ianlancetaylor ianlancetaylor changed the title proposal: go-2 support statically typed kwargs as an alternative to option structs proposal: Go 2: support statically typed kwargs as an alternative to option structs May 21, 2020
@ianlancetaylor ianlancetaylor added v2 A language change or incompatible library change LanguageChange labels May 21, 2020
@beoran
Copy link

beoran commented May 21, 2020

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 (
"fmt"
)

func Foo(p struct { I int ; S string ; }) {
fmt.Printf("%d %s \n", p.I, p.S)
}

func main() {
fmt.Println("Hello, playground")
Foo(struct { I int ; S string ; }{7, "Foo"})
}

@coolcodehere

This comment has been minimized.

@ianlancetaylor
Copy link
Contributor

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

@ianlancetaylor
Copy link
Contributor

No further comments.

@golang golang locked and limited conversation to collaborators Jun 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

7 participants