[Feature Suggestion] Enum inheritance

23 views
Skip to first unread message

Krzysztof Zoławy

unread,
Oct 16, 2022, 7:36:29 AM10/16/22
to Project Lombok
Hi,

I'd like to share with my idea to new lombok feature - enum inheritance.

As we now, in Java we cannot extends enums. In some cases this functionality would be very useful. Consider example:

With Lombok:

enum Position {
  TOP,
  BOTTOM,
  LEFT,
  RIGHT
}

@ExtendsEnum(Position.class)
enum ExtendedPosition {
  TOP_LEFT_CORNER,
  TOP_RIGHT_CORNER, 
  BOTTOM_LEFT_CORNER,
  BOTTOM_RIGHT_CORNER
}

Vanilla Java:

enum Position {
  TOP,
  BOTTOM,
  LEFT,
  RIGHT
}

enum ExtendedPosition {
  TOP,
  BOTTOM,
  LEFT,
  RIGHT,
  TOP_LEFT_CORNER,
  TOP_RIGHT_CORNER, 
  BOTTOM_LEFT_CORNER,
  BOTTOM_RIGHT_CORNER
}

Jan Materne

unread,
Oct 16, 2022, 9:53:17 AM10/16/22
to project...@googlegroups.com
I dont think that its that useful. Your extended enum could only used in your own code  On the other gand you would just copy a few lines..

Jan

--
You received this message because you are subscribed to the Google Groups "Project Lombok" group.
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombo...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/project-lombok/ca61d10d-3861-44e4-8d29-2ac8e71665ban%40googlegroups.com.

Mat Jaggard

unread,
Oct 16, 2022, 10:55:03 AM10/16/22
to project-lombok
Agreed. You'd also expect to be about to pass the extended enum into something that accepts the base enum like with object inheritance but that wouldn't work with this kind of fake inheritance - which is exactly why Java doesn't allow enums to inherit each other in the first place.

Floris Kraak

unread,
Oct 16, 2022, 4:38:59 PM10/16/22
to project...@googlegroups.com
If you really want to inherit some things (methods, obviously, since the values aren't really inheritable) - enums can implement interfaces which can implement default methods. 

So you could have both of those enums implement something like, say, a PositionalThingMabob interface and make the rest of the code not care about what position is involved exactly or where it's coming from that way.

Obviously Lombok doesn't need to be involved for that though.

Reply all
Reply to author
Forward
0 new messages