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

Use switch expressions where appropriate #28180

Closed
wilkinsona opened this issue Oct 2, 2021 · 9 comments
Closed

Use switch expressions where appropriate #28180

wilkinsona opened this issue Oct 2, 2021 · 9 comments
Assignees
Labels
status: superseded An issue that has been superseded by another type: task A general task

Comments

@wilkinsona
Copy link
Member

I don't think we make much use of switch statements, but we should see if there are places where using switch expressions will make the code more concise.

@wilkinsona wilkinsona added this to the 3.0.x milestone Oct 2, 2021
@wilkinsona wilkinsona added the status: blocked An issue that's blocked on an external project change label Oct 10, 2021
@Tharik67 Tharik67 mentioned this issue Dec 16, 2021
@philwebb
Copy link
Member

philwebb commented May 5, 2022

I tried to use one today and got:

> Task :spring-boot-project:spring-boot-docs:aggregatedJavadoc FAILED
/Volumes/Data/projects/spring-boot/code30x/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/AotProcessor.java:137: error: switch expressions are not supported in -source 8
                return switch (kind) {
                       ^
  (use -source 14 or higher to enable switch expressions)
/Volumes/Data/projects/spring-boot/code30x/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/AotProcessor.java:138: error: switch rules are not supported in -source 8
                case SOURCE -> this.sourceOutput;
                            ^
  (use -source 14 or higher to enable switch rules)
2 errors

@wilkinsona
Copy link
Member Author

@philwebb I think I've fixed that in #31210. You should be able to use switch expressions on main now.

@dreis2211
Copy link
Contributor

I think there needs some work to be done on the formatting end:

Running :format turns this:

return switch (getErrorProperties().getIncludeBindingErrors()) {
     case ALWAYS -> true;
     case ON_PARAM -> getErrorsParameter(request);
     default -> false;
};

into this:

return switch (getErrorProperties().getIncludeBindingErrors()) {
case ALWAYS -> true;
case ON_PARAM -> getErrorsParameter(request);
default -> false;
};

Arguably, the missing indentation makes it harder to read.

Otherwise this is ready on my local branch.

@wilkinsona
Copy link
Member Author

Thanks, @dreis2211. I prefer the pre-formatted version too. We can change this in Spring Java Format with this setting:

org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_switch=true

It also affects a "traditional" switch. This:

protected boolean isIncludeBindingErrors(HttpServletRequest request, MediaType produces) {
    switch (getErrorProperties().getIncludeBindingErrors()) {
    case ALWAYS:
        return true;
    case ON_PARAM:
        return getErrorsParameter(request);
    default:
        return false;
    }
}

Will become this:

protected boolean isIncludeBindingErrors(HttpServletRequest request, MediaType produces) {
    switch (getErrorProperties().getIncludeBindingErrors()) {
        case ALWAYS:
            return true;
        case ON_PARAM:
            return getErrorsParameter(request);
        default:
            return false;
    }
}

@dreis2211
Copy link
Contributor

@wilkinsona thanks.

I will hold my changes back until this is done. Could we maybe assign this issue to me, so nobody starts to work on it in parallel?

@philwebb
Copy link
Member

Assigned

@wilkinsona
Copy link
Member Author

@dreis2211 No rush whatsoever, but we've improved the formatting of switches so I think we're now ready when you are.

@wilkinsona wilkinsona removed the status: blocked An issue that's blocked on an external project change label Jun 24, 2022
dreis2211 added a commit to dreis2211/spring-boot that referenced this issue Jun 24, 2022
@dreis2211
Copy link
Contributor

There you go #31527 @wilkinsona

@wilkinsona
Copy link
Member Author

Thanks, Christoph. Closing in favor of #31527.

@wilkinsona wilkinsona closed this as not planned Won't fix, can't repro, duplicate, stale Jun 24, 2022
@wilkinsona wilkinsona removed this from the 3.0.x milestone Jun 24, 2022
@wilkinsona wilkinsona added the status: superseded An issue that has been superseded by another label Jun 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: superseded An issue that has been superseded by another type: task A general task
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants