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

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

Closed
gopherbot opened this issue Nov 23, 2009 · 3 comments
Closed

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

gopherbot opened this issue Nov 23, 2009 · 3 comments

Comments

@gopherbot
Copy link

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";
    }
@gopherbot
Copy link
Author

Comment 1 by bslesinsky:

Never mind. The correct way to write this is:
type Color bool;
const (
    Black = Color(false);
    White = Color(true);
)
func (c Color) String() string {
    switch (c) {
    case White: return "White";
    case Black: return "Black";
    }
    panic("not reachable");
}

@gopherbot
Copy link
Author

Comment 2 by eadfrith:

The types Color and bool are assignment compatible so you don't really need the
conversion - you just need to specify the Color type in the const declaration:
const (
  Black, White Color = false, true;
)

@griesemer
Copy link
Contributor

Comment 3:

Issue closed - this is not a bug. The code below works as expected.
package main
type Color bool;
const (
    Black Color = false;
    White Color = true;
)
func (c Color) String() string {
    switch c {
    case White: return "White";
    case Black: return "Black";
    }
    panic("not reachable");
}
func main() {
    println(Black.String());
    println(White.String());
}

Owner changed to g...@golang.org.

Status changed to Invalid.

This issue was closed.
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

2 participants