Skip to content

Suggestion: Record-style syntax for enum classes #700

Open
@EotT123

Description

@EotT123

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;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions