← Back to context

Comment by vips7L

4 months ago

What language are you using that doesn’t have match? Even Java has the equivalent. The only ones I can think of that don’t are the scripting languages.. Python and JS.

Does Java have sum types now?

  • Yes via sealed classes. It also has pattern matching.

    • So they are there, but ugly to define:

          public abstract sealed class Vehicle permits Car, Truck {
            public Vehicle() {}
          }
      
          public final class Truck extends Vehicle implements Service {
            public final int loadCapacity;
      
            public Truck(int loadCapacity) {
              this.loadCapacity = loadCapacity;
            }
          }
      
          public non-sealed class Car extends Vehicle implements Service {
            public final int numberOfSeats;
            public final String brandName;
      
            public Car(int numberOfSeats, String brandName) {
              this.numberOfSeats = numberOfSeats;
              this.brandName = brandName;
            }
          }
      
      

      In Kotlin it's a bit better, but nothing beats the ML-like langs (and Rust/ReScript/etc):

          type Truck = { loadCapacity: int }
          type Car = { numberOfSeats: int, brandName: string }
          type Vehicle = Truck | Car

      4 replies →

Python has it as well.

  • Ah my mistake. It’s been at least 5 years since I’ve written it. I’m honestly surprised that JS has moved no where on it considering all of the fancy things they’ve been adding.