>From the spec:
> If the switch expression evaluates to an untyped constant, it is first > converted to its default type; if it is an untyped boolean value, it is > first converted to type bool. The predeclared untyped value nil cannot be > used as a switch expression. The current behavior is still as specified. Given that there is an explicit statement about how this is handled makes it less convincing, however, that the spec would be more complicated with a different behavior. So you might still file a language change request, if you want. On Fri, Feb 2, 2018 at 4:39 PM, <d...@veryhaha.com> wrote: > > > On Friday, February 2, 2018 at 10:37:24 AM UTC-5, di...@veryhaha.com > wrote: > >> >> >> On Friday, February 2, 2018 at 10:27:04 AM UTC-5, Ian Lance Taylor wrote: >>> >>> On Fri, Feb 2, 2018 at 7:10 AM, <di...@veryhaha.com> wrote: >>> > >>> > Why not make it untyped? >>> > >>> > package main >>> > >>> > type T bool >>> > >>> > func f() T {return T(false)} >>> > >>> > func main() { >>> > switch { >>> > case f(): // invalid case f() in switch (mismatched types T and >>> bool) >>> > } >>> > } >>> >>> The current language spec says that omitting the switch expression is >>> equivalent to writing `true`. If you actually write `true`, then, >>> following the usual rules for untyped constant expressions, it will >>> receive the type `bool`, and you would get the same mismatched type >>> error. You will see a similar case if you write `switch 0` and try to >>> compare with a named version of `int`. So the compiler is following >>> the language spec as currently written. >>> >>> We could change the language spec to say that omitting the switch >>> expression is not equivalent to writing `true`, but instead, as you >>> suggest, compares each case to an untyped `true` value. I think the >>> main argument against that is that it makes the spec slightly more >>> complicated while bringing very little benefit. You could write it up >>> as a language change proposal if you like. >>> >>> Ian >>> >> >> > If you actually write `true`, then, >> > following the usual rules for untyped constant expressions, >> > it will receive the type `bool`, >> >> But, in my expression, the result of a constant expression is still >> untyped. >> And "true" is a per-declared untyped boolean value. >> >> And, here is another example to should the untyped result is converted to >> type "bool": >> > and sorry again, here "should", I mean "show". > > >> >> package main >> >> type T bool >> >> func f() T {return T(false)} >> >> func main() { >> switch true == true { >> case f(): // invalid case f() in switch (mismatched types T and bool) >> } >> } >> >> >> -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.