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: anonymous struct and array literals for functions #27543

Closed
koblas opened this issue Sep 6, 2018 · 3 comments
Closed

proposal: anonymous struct and array literals for functions #27543

koblas opened this issue Sep 6, 2018 · 3 comments

Comments

@koblas
Copy link

koblas commented Sep 6, 2018

Summary:
Allow for anonymous types for struct and array be passed to functions.

type Options struct {
    Name string
    Age  int
}

func SomeThing(options Options) {
    fmt.Printf("Got options %+v\n", options)
}

func AnotherThing(list []string) {
    fmt.Printf("Got list %v\n", list)
}

func main() {
    SomeThing({Name: "John Smith"})

    AnotherThing({"one", "two"})
}

Motivation:

Presently in go, there is auto typing available on function returns. value := MyFunc(). It would follow that given a known type on a function eg func SomeThing(options Options) that passing a structure that meets the type description required by the function should allow you to avoid presenting the full type name.

If you assign a struct/slice to a variable you would need to present a type.

value := { Name: "John Smith" }  // Not allowed, since it's ambigious

Generally trying to avoid having to write:

   value := module.SomeBigFunction(module.SomeReallyLongType{ .... })

Alternative:

Since having the ability to call functions with similar syntax {} values. The alternative would be to flag it as an anonymous struct or array by adding a struct{ .... } which still allows for the caller to quickly write compatible calls for interfaces without having to add fully qualified type descriptors.

func main() {
    SomeThing(struct{Name: "John Smith"})

    AnotherThing([]{"one", "two"})  // potentially simillary matched with an anon array
}

Cons:

The biggest loss is that you no longer have the explicit type name in the call to the function.

@gopherbot gopherbot added this to the Proposal milestone Sep 6, 2018
@cespare
Copy link
Contributor

cespare commented Sep 6, 2018

This essentially looks like #12854, which is more fleshed out and has seen a lot of discussion.

@dsnet
Copy link
Member

dsnet commented Sep 6, 2018

Hi, thank you for the proposal, but this looks to be a duplicate of what's proposed in #12854.

@dsnet dsnet closed this as completed Sep 6, 2018
@koblas
Copy link
Author

koblas commented Sep 6, 2018

You're right, didn't find that when I was looking.

@golang golang locked and limited conversation to collaborators Sep 6, 2019
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