-
Notifications
You must be signed in to change notification settings - Fork 185
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
Use of Optionals in Java 8 #94
Comments
I think this is a welcome change. First of all, and quite evidently, changing existing signatures will cause compatibility problems. So it should be done through new methods. However, I don't know if this is justified. The problem that having a default value returned does not tell you whether there was a value or not is solved by simply checking for member existence. Additionally, I'd rather read a check for presence with has... rather than a test to see if an optional is empty or not. |
What I meant, is the others methods than plain simple
All of which are very convenient. Optional official documentation |
It would be great to have these in the API, but retain the original methods too, which has advantages of not breaking existing API and also avoiding object creation when a simple null check will suffice. |
That would mean adding methods like |
Yes, that was how I understood the suggestion - I would assume in each case the actual method would be something very simple like
|
Mostly, with some differences for primitives because they cannot be null. |
Interesting point. Most of those methods have a default value as a parameter, so would you just put that into e.g. an
(which looks a bit pointless?!), or would you provide a new method something like:
(which looks more useful!) Or, I suppose you could do both..? |
I was thinking about the second version. |
Also, because the methods
This way, both ways can cohabit nicely in an intuitive manner? |
Well, based on this discussion, I'll open a pull request this week with it |
I am currently working on it, and I stumbled upon a problem: something changed between Java 7 and Java 8 in the language specifications related to type inference. For example, if I write:
the compiler recognizes I have tried this with the exact same code in a project with only the source code and not the project configuration and it works fine, so I'm sure it comes from the project configuration. NetBeans has hinted that it's related to |
I think you should be able to use
based on pg 138 of Joshua Bloch's 'Efficient Java' |
It works, thanks! |
Since Java 8, Optionals were added to replace
null
when a method can not return a result, as seen inJsonObject
with the methodsget
,getInt
...Although the current use of default values is convenient, I think it would be good to allow the use of Optionals, as they allow more control.
I am ready to make the changes myself and submit a pull request, but I'm not sure if it's better, regarding this project's policy, to do it:
getOptional
,getIntOptional
), which can make the code a bit "messy" (and maybe deprecate the old ones)Do you think it's worth doing it, and if it is, which would be the best way (I mean, the most user-friendly way) of implementing it?
The text was updated successfully, but these errors were encountered: