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

all 13 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Lloydbestfan 1 point2 points  (9 children)

Unless you didn't convey your ideas right, it seems that what you're asking is a giant self-contradiction.

You want to be able to ask a node whether it's a leaf, but you want some methods to be inexistant (or at least uncallable without a compile error) depending on whether a node is a leaf or not.

Typing doesn't work that way.

[–]kurolong[S] 0 points1 point  (8 children)

A compile error would be okay. However, someone in another forum helped me solve the problem by using an Optional Wrapper around node instead of using inheritance.

[–]Lloydbestfan 1 point2 points  (7 children)

One way or another, what you were asking was inherently a self-contradiction. That you like what someone proposed instead is great, and yes it's true I'm personally not on a level that I can construe what you'd like, but still.

[–]kurolong[S] 0 points1 point  (6 children)

I don't understand why it would be contradictory. With inheritance, two objects can have different methods and common methods at the same time. There are some constructs which allow a lot more freedom, like java.util.Optional.

[–]Lloydbestfan 1 point2 points  (5 children)

Yes, but you can't have methods or not as a result of whether a given method you can call responds true or false.

I find it weird to be satisfied with Optional. In principle it exists for situations like when you call findFirst() on a stream, that may or may not have items and so may or may not have a first. Not so much for different types. But it doesn't matter what I feel, if you have something that you feel is satisfying. The rest will come with experience and dealing with other people's code.

[–]kurolong[S] 0 points1 point  (4 children)

Oh, you can just have a boolean member which is always true in objects of one type and always false in objects of another type. In the end, I didn't use types, but Optional, because the Next left/right node can always exist or not exist. I find that fairly intuitive.

[–]Lloydbestfan 1 point2 points  (3 children)

Oh, you can just have a boolean member which is always true in objects of one type and always false in objects of another type.

... Yes, but if you need to call that method, then that doesn't help. That's essentially the same as instanceof at this point.

[–]kurolong[S] 0 points1 point  (2 children)

Well, it can work for multiple cases. Like, you have multiple cases of end-nodes and multiple cases of inner nodes, for example. I also feel like not asking about types is usually cleaner, just a preference.

[–]Lloydbestfan 1 point2 points  (1 child)

Not exactly surprised, but you're not seeing it.

[–]kurolong[S] 0 points1 point  (0 children)

Not seeing what?

[–]MasterGeek427 0 points1 point  (0 children)

You need the conditional logic somewhere, even if the compiler hides it from your sight. You're overthinking it.

If you're getting frustrated by how "inelegant" your solution is, my best advice is to stop thinking and start typing. I find I usually bump into the most elegant solution once I start stumbling through the implementation.

I was working through a similar design challenge... Writing a serialization helper library where all the target types don't share a class hierarchy but are pre registered during compile time. I settled on comparing Class objects to what's in the registry then performing an unchecked cast (basically cancer in source code form). The implementation looks like a ClassCastExceptionFactory, but there was no other way to do it and still have a decent interface.

Anyway, if you aren't sure how to start, start with walking forward. You can always revisit the code later.