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: add init statement to switch cases #19774

Closed
navytux opened this issue Mar 29, 2017 · 6 comments
Closed

proposal: spec: add init statement to switch cases #19774

navytux opened this issue Mar 29, 2017 · 6 comments
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Milestone

Comments

@navytux
Copy link
Contributor

navytux commented Mar 29, 2017

Hello up there. Effective Go says:

It's therefore possible—and idiomatic—to write an if-else-if-else chain as a switch.

for if statement the specification allows to have simple statements inside if for declaring/initializing variables, e.g. this way:

if x := f(); x < 0 {
    ...
}

and it works for if-else-if-else too.

However given a long if-else-if-else construct, in presence of simple statements inside if, it is not possible to rewrite it as switch-case-case because specification does not allow simple statements inside case - only inside switch.

For example I wanted to write something like this:

        // use buffered data: start + forward
        case sb.pos <= pos && pos < sb.pos + len64(sb.buf):
                ...

        // emptry request (possibly not hitting buffer - do not let it go to real IO path)
        // `len(p) != 0` is also needed for backward reading from buffer, so this condition goes before
        case len(p) == 0:
                return 0, nil

        // use buffered data: tail + backward
        case posAfter := pos + len64(p);                                // <-- NOTE
                sb.pos < posAfter && posAfter <= sb.pos + len64(sb.buf):
                ....

and could not. It is of course possible to use if-else-if-else chain, but the switch form looks more reasonable, and once again, Effective Go says it is idiomatic to write such chains as switch.

So I propose to extend language specification to allow simple statements inside case.

Thanks beforehand,
Kirill

@gopherbot gopherbot added this to the Proposal milestone Mar 29, 2017
@bronze1man
Copy link
Contributor

I think a simple statements inside "if" is a bad idea. It too complex to understand that code.I never use that language feature.
So I think this one is a bad idea too.

@dcheney-atlassian
Copy link

I think a simple statements inside "if" is a bad idea. It too complex to understand that code.I never use that language feature.

if err := fn(); err != nil {
       // handle error
}

Is quite popular.

@bronze1man
Copy link
Contributor

bronze1man commented Mar 31, 2017

It Is quite popular indeed.
And we should do independent thinking.So I hate this language feature("if" simple statements).
It just cost a lot energy to understand that code.
Because I need to think is whether this err in a scope, it also too easy to just skip that fn function call.
By the way I hate "if"/"switch"/"for" variable scope language feature too.The php function scope stuff does the best.

I prefer:

err := fn()
if err != nil {
       // handle error
}

@rsc rsc added the v2 A language change or incompatible library change label Apr 3, 2017
@navytux
Copy link
Contributor Author

navytux commented May 23, 2017

I'd like to add: case already supports simple statements under select:

select {
case <-ch1:
    // ...
case x := <-ch2
    // ...use x
    ...
}

which is widely used in go stdlib itself:

kirr@deco:~/src/tools/go/go/src$ git grep -w case |grep ':=' | wc -l
142

For me this is simply language consistency fix, not go2 matter...

@cznic
Copy link
Contributor

cznic commented May 23, 2017

case already supports simple statements under select:

Send statement is the only simple statement a CommCase is specified to accept.

@rsc rsc changed the title proposal: spec: Allow simple statements in case; e.g. case x := ...; x < 0: doSomething() proposal: spec: add init statement to switch cases Jun 16, 2017
@ianlancetaylor
Copy link
Contributor

This makes the switch statement harder to understand when switching on a value. Consider

switch v {
case 0:
case v++; 3:
case 4:
}

While we can define exactly what this means, it is just confusing. The advantage of being able to write the statement does not seem worth the complexity cost.

@golang golang locked and limited conversation to collaborators Jan 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

8 participants