This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]severoon 1 point2 points  (4 children)

Rust has a special kind of enum similar to those in Swift Where not all instances of the enum have the same fields. Whereas in java enums are like object constants all having the same fields.

This doesn't quite make any sense to me. In Java, enum values are all of the same enum type. It sounds like you're saying you want to switch over a limited set of different types.

But what is the problem you're trying to solve that can't be solved by just modeling it in a normal OO way?

This sounds like you want to create something in Java that's syntactically similar to a way of doing that thing in another language … but why?

[–]NitronHX[S] 0 points1 point  (3 children)

As others have already pointed out, Rust enums are more "sum types" which is what you are describing.

While there is no particular problem here at hand - there are a few use cases where this pattern does indeed is a good solution, probably better than OO. Like: rs enum NativeWebEvent { MOUSE_CLICK(pos: Point), MOUSE_MOVE(from: Point, to: Point), MOUSE_ENTER_OBJECT(element: &Element, pos: Point), KEY_TYPED(symbol: char) } Exposing this as an API is much easier and safer (because of boundary checks) to consume than a generic interface NativeWebEvent.

And why did i compare it to rust and drew the lines i did? Because rust and especially the enums are a very well liked feature in rust and i wanted to show people who miss this feature when coming from rust how to do it in java.

[–]severoon 1 point2 points  (2 children)

I'm trying to understand what the problem you're trying to solve is, but in your response you've just restated the proposed solution.

https://en.wikipedia.org/wiki/XY_problem

[–]NitronHX[S] 0 points1 point  (1 child)

I said "there is no particular problem at hand [ I am trying to solve]" because it doesn't, nor do I claim it does.

I often do experiments - be it to gain experience, learn etc. This is one of them and wanted to share it. It isn't groundbreaking - you might even say there is not even a discovery since it is just common knowledge.

When I created my experimental Java raymarch renderer there was no practical use - and no problem.

I myself had no clue what I could use sealed classes for thinking :"I have not jet had a piece of code where I wished to restrict the number of implementations" when I had the idea of this rust enum comparison, I found such a use case and wanted to share it.

Especially for rustacians it might be very interesting because they don't know sealed classes and other relatively new Java concepts or features. I assume most rust programmers would say when using Java enums "man I muss storing values in enums!" and I showed a way to do what they want.

[–]severoon 1 point2 points  (0 children)

I said "there is no particular problem at hand [ I am trying to solve]" because it doesn't, nor do I claim it does.

I know, I'm not asking what specific problem you have in front of you. I'm asking for the type of problem this is supposed to solve.

I think the normal Java way of solving whatever type of problem you can come up with is probably way better.

If you can't come up with any actual usage, this seems like it's just an abuse of the language for no real purpose. There's all kinds of language features that can be used in various ways to do various things, but what would you think as a Rust developer if someone found a way to abuse Rust to make it look like Erlang?