What's the best I can get to an enum entry alias?
# announcements
d
What's the best I can get to an enum entry alias?
I guess an inline extension property.
e
What do you mean by enum entry alias?
d
Copy code
enum class SomeEnum {
    A, B, C
}

// Current solution
inline val SomeEnum.Companion.A_OLD: SomeEnum get() = SomeEnum.A

// Thus allowing deprecation/renaming of A_OLD to A
SomeEnum.A // Works
SomeEnum.A_OLD // Also works, even though it's not defined inside enum.
d
I dont believe that's binary compatible
d
Yeah, it's not. I'm considering just having both inside the enum.
d
You should probably add
@Deprecated
to the old thing, wherever you put it
Its effective because you can make code not compile against your new version by doing this without giving up binary compatibility
d
The enum has a
val value: UInt
property, both
A
and
A_OLD
have the same
value
and I was hoping to make them point to the same entry instead of just having the same
value
.
For my use case it's not for deprecation, it's literally just an alternative name for the entry.
d
Unfortunately, you cannot override
equals
in enums.
d
Maybe a sealed class instead?
d
You could go with simple properties?
d
Yes. It's only to provide type-safe enums for Vulkan or OpenGL. Anything better than loose global integers will do the trick tbh.
d
Ah, I've done some of that as well
I just use enums though, I'm not concerned if constants are the same
d
That would be fine if the enums were only used as inputs but in some cases the api returns an enum to the application (as an Int) and my wrapper has to look up the correct enum entry. Equality would have to work if the user needs to do anything with the enum value.