Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Once withers land, I think you could approximate this by letting your record class have a zero argument constructor which sets every field to some blank value, and then fill the fields using `with`.

  var x = new Car() with { make = "Volvo"; year = "2023"; };
If you want the Car constructor to enforce constraints, you could use this pattern in a separate Builder record:

  record Car(String make, String year) {
    Car {
      Objects.requireNonNull(make);
      Objects.requireNonNull(year);
    }

    record Builder(String make, String year) {
      Builder() {
        this(null, null);
      }
      Car build() {
        return new Car(make, year);
      }
    }
  }

  var x = new Car.Builder() with { make = "Volvo"; year = "2023"; }.build();
Obviously syntax TBD.


So much syntax to enable something that other languages have had for 10+ years. That's why I can't take the "Java is as good as Kotlin now" arguments seriously.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: