Skip to content
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

java.util.Optional support in the standard library #321

Open
ilya-g opened this issue Aug 26, 2022 · 4 comments
Open

java.util.Optional support in the standard library #321

ilya-g opened this issue Aug 26, 2022 · 4 comments
Labels

Comments

@ilya-g
Copy link
Member

ilya-g commented Aug 26, 2022

This issue is for discussion of the proposal to introduce convenience extensions for java.util.Optional type in the standard library.

@ilya-g ilya-g added the stdlib label Aug 26, 2022
@ilya-g ilya-g mentioned this issue Aug 26, 2022
@fvasco
Copy link

fvasco commented Aug 27, 2022

I never defined three distinct methods for unwrapping, in my experience Optional<T>.orNull() is enough.

#291 (comment)

@fvasco
Copy link

fvasco commented Aug 27, 2022

For completeness, we propose including the following alternatives to Optional's factory methods

I propose to reconsider this section, should we provide a dedicated constructor for this type, only for completeness?
A standard optionalOf method can create confusion with other types of Optionals, like Guava's provided one.
Kotlin does not provide a streamOf, completableFutureOf or a publisherOf.
What should be the type of optionalOf(3), OptionalInt or Optional<Int>?

I think that Optional.of* is enough, using Java syntax with Java classes is not so strange for me, factory methods can be shifted in future advancements if required.

#291 (comment)

@ilya-g
Copy link
Member Author

ilya-g commented Aug 28, 2022

I propose to reconsider this section

@fvasco please mind the note in the beginning of that section: it's excluded from the proposal.

@joffrey-bion
Copy link

joffrey-bion commented Aug 29, 2022

While the goal of such addition as stated in the KEEP would be to help with Java interop, I'm afraid providing this many helpers may encourage the use of Optional within Kotlin code - where nullable types are more idiomatic and benefit from nice and concise operators (and declarations).

Like @fvasco, I believe Kotlin only really needs .orNull(). This function is useful because the vast majority of interop with Java's Optional would be to convert Optional return values of Java functions into nullable Kotlin types, which simply transforms the Java signature into what you would have had from the Kotlin equivalent. Considerations of getOrElse etc. shouldn't really apply because in Kotlin, you would have received a nullable value in the first place, so after .orNull() conversion one can just write idiomatic Kotlin code.

Optional arguments in Java methods are quite rare in comparison to optional return values (there are arguments for avoiding such practice), so I see little benefit in providing factory methods for those, especially since there are existing static factory methods on Optional that do the job quite OK. IMO this part is the one that would encourage the most to create more Optional values in Kotlin, which I believe would be a step backwards. (EDIT: this has been excluded from the proposal as of now)

Also, Optionals are not supposed to represent collections, so the proposed conversion methods (toSet, toList, asSequence) seem semantically debatable. If we consider that we unwrap optionals from Java methods into nullable values, this is the end of the interaction with the Java API surface. We can then use nullable values as if they were returned by Kotlin methods - using other helpers such as listOfNotNull etc.

#291 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants