Skip to content

[java] Perform type inference on switch expressions

Problem to Solve

The Java indexer does not infer the type of a switch expression:

var promotion = switch (day) {
    case MONDAY, FRIDAY, SUNDAY -> new WinePromotion();
    case TUESDAY                -> new TarTarPromotion();
    case THURSDAY, SATURDAY     -> new BurgerPromotion();
    case WEDNESDAY              -> new SteakPromotion();
};
var price = promotion.applyOn(item);

This should create an edge between the current function and the applyOn function but it does not in the current version.

Proposed Solution

  • Parse switch expressions in the code-parser.
    • Include the last expression of each branch in the switch expression to perform type inference.
  • In the indexer, find all the return types of the branches and find the common type ancestor.