Go: proposal: spec: add init statement to switch cases

Created on 29 Mar 2017  路  6Comments  路  Source: golang/go

Hello up there. Effective Go says:

It's therefore possible鈥攁nd idiomatic鈥攖o 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

FrozenDueToAge Go2 LanguageChange Proposal

Most helpful comment

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.

All 6 comments

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.

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.

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
}

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...

case already supports simple statements under select:

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

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jayhuang75 picture jayhuang75  路  3Comments

gopherbot picture gopherbot  路  3Comments

longzhizhi picture longzhizhi  路  3Comments

mingrammer picture mingrammer  路  3Comments

stub42 picture stub42  路  3Comments