-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Java 14+ pattern matching in
instanceof
wherever applicable + m…
…ove `com.virtuslab.qual.{gitmachete => subtyping.gitmachete}`
- Loading branch information
1 parent
80b19a5
commit e40d1da
Showing
14 changed files
with
65 additions
and
23 deletions.
There are no files selected for viewing
4 changes: 0 additions & 4 deletions
4
qual/src/main/java/com/virtuslab/qual/internal/package-info.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
qual/src/main/java/com/virtuslab/qual/subtyping/internal/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* Module private. Do not use outside {@link com.virtuslab.qual.subtyping}. | ||
*/ | ||
package com.virtuslab.qual.subtyping.internal; |
54 changes: 48 additions & 6 deletions
54
qual/src/main/java/com/virtuslab/qual/subtyping/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,53 @@ | ||
/** | ||
* Java 17 allows for pattern matching in switch expressions & statements | ||
* (https://www.baeldung.com/java-switch-pattern-matching). | ||
* Still, a quick evaluation shows that Subtyping Checker is more convenient: | ||
* <ol> | ||
* <li>{@code instanceof} requires introducing a new variable</li> | ||
* <li>{@code instanceof} only detects one subtype, not the other</li> | ||
* <li>{@code instanceof} requires sealed classes/interfaces for exclusivity check to work; this is problematic for interfaces like I... coz Base... </li> | ||
* </ol> | ||
* Still, a quick evaluation shows that Subtyping Checker is more convenient. | ||
* Let's consider the example of {@code com.virtuslab.gitmachete.backend.api.IManagedBranchSnapshot}, | ||
* with its two sub-interfaces {@code IRootManagedBranchSnapshot} and {@code INonRootManagedBranchSnapshot}. | ||
* <p> | ||
* With Subtyping Checker, the following is possible: | ||
* <pre> | ||
* if (branch.isRoot()) { | ||
* ... branch.asRoot() ... | ||
* } else { | ||
* ... branch.asNonRoot() ... | ||
* } | ||
* </pre> | ||
* <p> | ||
* With {@code instanceof}, it would look like: | ||
* <pre> | ||
* if (branch instanceof IRootManagedBranchSnapshot rootBranch) { | ||
* ... rootBranch ... | ||
* } else { | ||
* ... ((INonRootManagedBranchSnapshot) branch) ... | ||
* } | ||
* </pre> | ||
* or alternatively | ||
* <pre> | ||
* if (branch instanceof IRootManagedBranchSnapshot rootBranch) { | ||
* ... rootBranch ... | ||
* } else if (branch instanceof INonRootManagedBranchSnapshot nonRootBranch) { | ||
* ... nonRootBranch .. | ||
* } | ||
* </pre> | ||
* <p> | ||
* Pattern matching in switch (Java 17 with {@code --enable-preview}) | ||
* won't work for interfaces that are directly extended by an abstract class | ||
* (and not only by sub-interfaces), as is the case with {@code IManagedBranchSnapshot}. | ||
* The existence of {@code com.virtuslab.gitmachete.backend.impl.BaseManagedBranchSnapshot} would massively mess up the checks | ||
* for sealed interface, which are needed for pattern matching to work properly. | ||
* We can either sacrifice sealedness of {@code IManagedBranchSnapshot}: | ||
* <pre> | ||
* switch (branch) { | ||
* case IRootManagedBranchSnapshot rootBranch -> ... | ||
* case INonRootManagedBranchSnapshot nonRootBranch -> ... | ||
* // WHOOPS compiler sees this match as non-exhaustive | ||
* } | ||
* </pre> | ||
* or include {@code BaseManagedBranchSnapshot} in {@code permits} clause for {@code IManagedBranchSnapshot}, | ||
* which would also cause the compiler to report a non-exhaustive {@code switch} error. | ||
* <p> | ||
* All things consider, as of Java 17, Subtyping Checker remains a cleaner choice for exhaustive matching on subtypes | ||
* than whatever mechanism built into the language. | ||
*/ | ||
package com.virtuslab.qual.subtyping; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters