Skip to content

switch doesn't work for a type derived from bool #312

Closed
@gopherbot

Description

@gopherbot

by bslesinsky:

What steps will reproduce the problem?

type Color bool;
const (
    Black = false;
    White = true;
)

func (c Color) String() string {
    switch c {
    case White: return "White";
    case Black: return "Black";
    }
    panic("not reachable");
}


What is the expected output? What do you see instead?

I think this should compile, but I get: 

gongo.go:66: case true (type bool) in Color switch
gongo.go:66: case false (type bool) in Color switch


What is your $GOOS?  $GOARCH?

$ echo $GOOS $GOARCH
darwin 386

Which revision are you using?  (hg identify)

$ hg identify
44699e529c44+ tip

An easy workaround is to rewrite it like this:

    switch {
    case c == White: return "White";
    case c == Black: return "Black";
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions