3 ms·
> I always wondered why enums in java were allowed to be null. Because they’re just the “type-safe enum” pattern, built in, with some compiler support (e.g. sw
by masklinn 4y ago
> I always wondered why enums in java were allowed to be null.
Because they’re just the “type-safe enum” pattern, built in, with some compiler support (e.g. switches). Besides that they’re normal reference types.
> Could have just had some sort of 'default' attribute on a value.
Universal defaults are, if anything, even worse than universal nulls: they generate garbage states you can’t guard against at all.
- mbfg 4y agoJust trying to learn here, not being combative, couldn't the same compiler support have just enforced the non nullness? At first you think, how do you enforce someEnum = getAnEnumSomewhere(), but at some point there's got to be an original reference being hard-set to null that the compiler could see, right? also i don't understand your point about universal defaults? enum Foo { bar default, baz }; why can't you guard against the default? how is checking a foo enum against bar different then checking against null?