3 ms·
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
by vips7L 1y 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.
- lock1 1y ago[dead]
- tonyedgecombe 1y agoDoes Java have sum types now?
- palata 1y agoI think Java 21 does. Scala and Kotlin do as well.
- vips7L 1y agoYes via sealed classes. It also has pattern matching.
- cies 1y agoSo 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
- weksf54 1y agoYou could use Java records to make things more concise: record Truck(int loadCapacity) implements Vehicle {} record Car(int numberOfSeats, String brandName) implements Vehicle {} sealed interface Vehicle permits Car, Truck {}
- vips7L 1y agoYou implemented this much more verbosely than needed sealed interface Vehicle { record Truck(int loadCapacity) implements Vehicle {} record Car(int numberOfSeats, String brandName) implements Vehicle {} }
- weksf54 1y agoAh! Thanks, I didn't know that. I should have RTFMD better - https://docs.oracle.com/en/java/javase/21/language/sealed-classes-and-interfaces.html https://docs.oracle.com/en/java/javase/21/language/sealed-cl... Turns out you can do this and not have the annoying inner class e.g. Vehicle.Car too: package com.example.vehicles; public sealed interface Vehicle // The permits clause has been omitted // as its permitted classes have been // defined in the same file. { } record Truck(int loadCapacity) implements Vehicle {} record Car(int numberOfSeats, String brandName) implements Vehicle {}
- Tainnor 1y agoScala 3 has: enum Vehicle: case Truck(loadCapacity: Int) case Car(numberOfSets: Int, brandName: String)
- pjmlp 1y agoPython has it as well.
- vips7L 1y agoAh 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.
- pjmlp 1y agoIt has been proposed, but since there is all the process on how features get added into the standard, someone needs to champion it, and then there is the "at least two implementations" factor. https://github.com/tc39/proposal-pattern-matching https://github.com/tc39/proposal-pattern-matching