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
mmacheerpuppy opened this issue May 20, 2020 · 6 comments

Comments

@mmacheerpuppy
Copy link

@mmacheerpuppy mmacheerpuppy 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
@gopherbot gopherbot added the Proposal label May 20, 2020
@mmacheerpuppy mmacheerpuppy 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
@mmacheerpuppy mmacheerpuppy 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
@mmacheerpuppy mmacheerpuppy 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 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

@jimmyfrasche jimmyfrasche commented May 21, 2020

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
@beoran
Copy link

@beoran 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 was marked as off-topic.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented May 26, 2020

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

@ianlancetaylor ianlancetaylor commented Jun 23, 2020

No further comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
7 participants
You can’t perform that action at this time.