Strings in Switch

Neal Gafter neal at gafter.com
Sun Dec 6 20:38:33 PST 2009


On Sun, Dec 6, 2009 at 7:38 PM, Reinier Zwitserloot <reinier at zwitserloot.com
> wrote:

> 'hello' and 'world' have different hashes, so it seems we can just desugar
> this to:
>
> switch(x.hashCode()) {
>   case "hello".hashCode(): doX(); break;
>   case "world".hashCode(): doY(); break;
> }
>
>
> but that's not the right desugaring, because if I return some string that
> ISNT "hello" but so happens to hashCode to the same hashCode as "hello",
> then we just broke the program.
>

I think Paul is imagining compiler-generated if statements inside the cases
to check string.equals(x, "hello") etc.

Cheers,
Neal



More information about the coin-dev mailing list