New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added method ifExists for ease of use #137
Conversation
configurate-core/src/main/java/ninja/leaping/configurate/ConfigurationNode.java
Outdated
Show resolved
Hide resolved
Perhaps make this return the same node
.ifVirtual(thing -> ....)
.ifExists(thing -> ...); I'm also not sold on the |
Just want to start by saying this is really just an opinion and to not consider this a rejection. I'm not convinced about this in this form because this could very quickly turn into being a mapping function by accident. These lambdas were never intended to be pure functions by any means but they shouldn't be affecting the object that calls them in this instance. The problem is that, generally, I would have less of a problem if changes made to the node in the function weren't reflected in the node that called the function. We could do this if we make a I see the problem as to why you chose to return the While I like using functional forms, it doesn't seem right to do so here, particularly if you want to modify the basic node after - if that's the case, you really should stick to the standard if statements. Otherwise, I would advise you looking at making this either a mapping function If this goes ahead as is, the javadocs must be crystal clear as to how this works and, if it remains like this, any mutations to the configuration node inside the lambda will be reflected in the node itself, that it is not a copy. EDIT: To clarify - I'm not going to lose sleep over this and I'm not stopping this, my point is from a more pure point of view. As long as it's clear what the method does (particularly about the mutation of the node) I'm not going to cry foul. |
Any suggestions as an alternative to ifExists? I asked in the discord and that was the consensus but am open to whatever is most descriptive. |
That brings up a good point. The point to this originally was never to make a copy but actually to make mutating the node easier. I’ll look into some alternatives like your example of mapIfVirtual() but otherwise I will spruce up the javadocs to be clear it isn’t just a copy but the node itself. |
Honestly, copying isn't really what we want to do here because it's just too expensive. I don't particularly like the idea of the mutating lambdas, but if we name it something else and clearly doc it, I'd settle for that. To that end, we should go for Also, while I can understand the idea behind the chaining idea, consider this: node.whenVirtual(node -> node.setValue(42))
.whenPresent(node -> System.out.println(node.getValue())); If you simply implement this in the way your PR does it now, both If you wanted to do that in a functional style, it would be better to simply specify |
I’d argue the idea of using (not replacing entirely) if statements with a function Take for example the following example: if(rootNode.getNode("Message").isVirtual()) {
Text message = Text.of(rootNode.getNode("Message).getValue());
player.sendMessage(message);
} That isn't a huge amount of work but that can all be merged into rootNode.getNode("Message").whenPresent(msg -> player.sendMessage(Text.of(msg)); It isn't a massive change but can be used in a lot of ways and can shorten up hundreds of lines of code if there are lots of node.getNode("Item#, Name).whenPresent(node -> stack.offer(Keys.DISPLAY_NAME, node.getString()); I could do that for each new offer and cut down the code a bit without sacrificing readability or any extra resources. In terms of the mutability, if the javadocs clearly defined that it does get the actual node and not just a copy of the node, it shouldn't create problems for anyone. This allows for the programmer to still easily use the lambda function without having to expend resources with another I'd say that although the PR is a small change, it can really cut down on unwanted if statements everywhere. The push for more functional workings with Java is great and can be used to simplify a lot without writing extra methods which is why I made this PR in the first place. |
I get what you're trying to do and why. I never said I didn't. I said I disagree with this solution, but have also provided mitigations and thoughts on alternatives - and that I'm not going to lose sleep over this. I've made my points that I stand by and I don't really want to let this issue spiral out - especially as I have already said in a previous comment that I will settle for clear documentation and I wasn't going to fight against it. |
I disagree with this PR.
|
I generally do use h2 DBs for storage and many of my large plugins are using ConfigSerializable for the config anyway. This was just a solution for a much more dynamic plugin that requires quick checks without having to setup a mapper. However this seems to be pretty contested and I can live without it so I'll take down this PR. |
Method takes in a consumer so users can easily check if a node is not virtual and work with it without having to separate things into if statements.
All tests created passed on my end but can be tested again if wanted