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

generate a switch whenever possible #2398

Open
scabug opened this issue Sep 26, 2009 · 3 comments
Open

generate a switch whenever possible #2398

scabug opened this issue Sep 26, 2009 · 3 comments

Comments

@scabug
Copy link

scabug commented Sep 26, 2009

(I'm consolidating related tickets: see also #1452, #1456, #1457, #1458, #6955, #6956)

Terms:

  • "intlike type" is any of Int, Short, Byte, or Char.
  • "intlike" means an intlike literal or intlike identifier.
  • "intlike literal" means any literal constant with an intlike type.
  • "intlike identifier" means an identifier which can be statically determined to have the following properties:
    • immutable (i.e. a val)
    • not overridable (i.e. final or enclosed in a final container)
    • assigned a value which can be statically reduced to an intlike literal. This means arithmetic operations only, on intlikes.

scala should generate tableswitch/lookupswitch bytecode whenever the following conditions hold:

  • a) the scrutinee is any expression with an intlike result type.
  • a1) or, if b) and c) hold, then if the scrutinee is AnyVal or Any, a switch can still be generated by inserting if/then/else logic before the switch.
  • b) all non-default cases are intlike.
  • c) if there is a default case, it either has no type annotation, or a type annotation which matches all intlike values.

At present, an identifier in a match statement will suppress the creation of a switch statement 100% of the time if it makes it to the pattern matcher stage. When scala's constant folder replaces the identifier with the constant before reaching that point, the switch can be created; but this takes place only in a fraction of the cases where it's desirable.

@scabug
Copy link
Author

scabug commented Sep 26, 2009

@scabug
Copy link
Author

scabug commented Jan 28, 2014

@adriaanm said:
also applies when scrutinee is value class that unboxes to switchable type

@SethTisue
Copy link
Member

this ticket seems not to have been looked at in a long time. anyone know what the current state is, in Scala 2 and/or 3? not sure whether to add the "help wanted" label and/or the "fixed in Scala 3" labels

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants