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: spec: implicit types and values in variable declaration blocks #20951

Closed
willfaught opened this issue Jul 7, 2017 · 12 comments
Closed

Comments

@willfaught
Copy link
Contributor

A small suggestion for Go 2:

Allow implicit types and values in variable declaration blocks like there can be for constant declaration blocks. For example, this works:

const (
	x int = 5
	y
)

but this doesn't:

var (
	x int = 5
	y // syntax error: unexpected semicolon or newline
)

Allowing implied zero values would also be useful:

var (
	x int
	y
)
@gopherbot gopherbot added this to the Proposal milestone Jul 7, 2017
@davecheney
Copy link
Contributor

davecheney commented Jul 8, 2017 via email

@willfaught
Copy link
Contributor Author

@davecheney If you were to do that then you couldn't also document each one individually, e.g.

var (
	// X...
	X int

	// Y...
	Y
)

@willfaught
Copy link
Contributor Author

The two forms of declaration aren't equivalent, so I don't think redundancy is a concern here.

@davecheney
Copy link
Contributor

davecheney commented Jul 8, 2017 via email

@willfaught
Copy link
Contributor Author

You could similarly just write:

var x int
var y int

instead of:

var x, y int

so why not drop that superfluous syntax? In my opinion, this falls under the concerns of clarity and consistency of expression.

Example of exported use:

var (
	// Dry...
	Dry bool

	// Quiet...
	Quiet

	// Verbose...
	Verbose

	// Sudo...
	Sudo

	// Home...
	Home string

	// User...
	User

	// Host...
	Host

	// ...
)

Example of unexported use:

var (
	i int // i...
	j     // j...
	k     // k...

	x float64 // x...
	y         // y...
	z         // z...

	// ...
)

@willfaught
Copy link
Contributor Author

Along that same consistency line, it seems to me that it would actually simplify the language if the syntax were even more similar for both constant and variable blocks.

@davecheney
Copy link
Contributor

davecheney commented Jul 8, 2017 via email

@willfaught
Copy link
Contributor Author

I don't think I'm making that mistake; this is the first time you've mentioned that concern. Regarding readability, it seems to me that both constant and variable block declarations can have types, so if it makes sense to have "stamped" types for constants, I don't see why it doesn't make sense for variables too. Is there a reason you can think of to treat them differently in this respect? It seems like the only difference between the two syntaxes should be initialization.

@davecheney
Copy link
Contributor

davecheney commented Jul 8, 2017 via email

@willfaught
Copy link
Contributor Author

Good point, I can see that. I wonder whether it would be useful at all for var block decls to have type/expr "stamps" too, but that's a separate issue.

@Allthewaylive247
Copy link

Can anyone help me out this is too smart for me

@willfaught
Copy link
Contributor Author

@Allthewaylive247 What do you mean?

@golang golang locked and limited conversation to collaborators Jul 18, 2018
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