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: consider if and switch as an expression #68413

Closed
4 tasks
Avinash-Bhat opened this issue Jul 14, 2024 · 2 comments
Closed
4 tasks

proposal: Go 2: consider if and switch as an expression #68413

Avinash-Bhat opened this issue Jul 14, 2024 · 2 comments
Labels
LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change
Milestone

Comments

@Avinash-Bhat
Copy link

Go Programming Experience

Novice

Other Languages Experience

Java, Python, Kotlin, JS

Related Idea

  • Has this idea, or one like it, been proposed before?
  • Does this affect error handling?
  • Is this about generics?
  • Is this change backward compatible? Breaking the Go 1 compatibility guarantee is a large cost and requires a large benefit

Has this idea, or one like it, been proposed before?

No, but not 100% sure

Does this affect error handling?

No.

Is this about generics?

No.

Proposal

This is a backward-compatible proposal to allow branching statement like if or switch to be used as an expression to be used for assignments.

This should reduce verbosity of code while keeping the impact minimal. It can also be used by static analysis tools to find unhandled scenarios with iota transformations.
Also, it would inadvertantly mean that go would have a ternary operator.

Consider the following code to find the maximum of two numbers:

package main

import "fmt"

func main() {
	a := 1
	b := 2

	var max int

	if a > b {
		max = a
	} else {
		max = b
	}

	fmt.Printf("max: %d\n", max)
}

with an if expression, it can be simplified to:

package main

import "fmt"

func main() {
	a := 1
	b := 2

	max := if a > b {
		a
	} else {
		b
	}

	fmt.Printf("max: %d\n", max)
}

Some similar proposals would get an alternative syntax to be implemented:

Language Spec Changes

No response

Informal Change

No response

Is this change backward compatible?

Mostly. Haven't though about the whole picture

Orthogonality: How does this change interact or overlap with existing features?

No response

Would this change make Go easier or harder to learn, and why?

It would reduce the verbosity of the code and add an existing language feature of Python and Kotlin.

Cost Description

No response

Changes to Go ToolChain

gofmt, gopls, vet.

Performance Costs

No response

Prototype

No response

@Avinash-Bhat Avinash-Bhat added LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change labels Jul 14, 2024
@gopherbot gopherbot added this to the Proposal milestone Jul 14, 2024
@seankhliao
Copy link
Member

Duplicate of #44022

@seankhliao seankhliao marked this as a duplicate of #44022 Jul 14, 2024
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jul 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change
Projects
None yet
Development

No branches or pull requests

4 participants