Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rendering Enums should call name(), instead of toString() #185

Closed
casid opened this issue Nov 19, 2022 · 0 comments
Closed

Rendering Enums should call name(), instead of toString() #185

casid opened this issue Nov 19, 2022 · 0 comments
Assignees

Comments

@casid
Copy link
Owner

casid commented Nov 19, 2022

Enums are usually used to provide values to input elements that are then submitted to the backend. For example:

public enum Car {
    Volvo,
    Saab,
    Fiat,
    Audi,
}
<select id="cars" name="cars">
    @for(Car car : Car.values())
        <option value="${car}">${localize(car)}</option>
    @endfor
</select>

This will break unexpectedly, as soon as someone overrides the toString() method:

public enum Car {
    Volvo,
    Saab,
    Fiat,
    Audi,
    ;
    
    @Override
    public String toString() {
        return name() + " is a fine car!";
    }
}

To prevent this from happening, rendering Enums should call name(), instead of toString().

@casid casid self-assigned this Nov 19, 2022
@casid casid closed this as completed Nov 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant