manifold icon indicating copy to clipboard operation
manifold copied to clipboard

Suggestion: Record-style syntax for enum classes

Open EotT123 opened this issue 11 months ago • 1 comments

It would be great if Manifold could support a more concise, record-style syntax for enum classes that have parameters and corresponding getters.

Currently, defining such enums requires boilerplate code like this:

public enum Day() {
    MONDAY(1, "Monday"),
    TUESDAY(2, "Tuesday"), ...

    private final int dayOfTheWeek;
    private final String value;
 
    Day(int dayOfTheWeek String value){
        this.dayOfTheWeek = dayOfTheWeek;
        this.value=value;
    }

    public int getDayOfTheWeek(){
       return dayOfTheWeek;
    }

    public String getValue(){
       return value;
    }
}

Similar to how records reduce boilerplate, this could be rewritten as:

public enum Day(int dayOfTheWeek, String value) {
    MONDAY(1, "Monday"),
    TUESDAY(2, "Tuesday"), ...
}

As with records, developers should still be able to define a custom constructor if needed to improve flexibility:

public enum Day(int dayOfTheWeek, String value) {
    MONDAY(1, "Monday"),
    TUESDAY(2, "Tuesday"), ...

    Day(int dayOfTheWeek String value){
        this.dayOfTheWeek = dayOfTheWeek;
        this.value = dayOfTheWeek + " - " + value;
    }
}

EotT123 avatar Apr 28 '25 20:04 EotT123

Nice! Adding optional parameters would be nice here as well.

Note, a similar feature is on the tarmac for struct classes.

rsmckinney avatar Apr 29 '25 04:04 rsmckinney