Details
-
Improvement
-
Status: Closed
-
Trivial
-
Resolution: Fixed
-
None
Description
The JDK recently added support for Optional::stream() which returns a stream containing either the unwrapped value or an empty stream if the Optional is empty.
In the groovy-jdk, using iteration however will call
DefaultTypeTransformation.asCollection()
which does not specifically check for type Optional, instead it will just wrap the Optional itself in a List rather than the unwrapped value as the default behavior.
Adding an if else clause will allow using the Optional as either a single or empty list.
if (value instanceOf Optional) { return ((Optional)value).map(List::of).orElse(List.of()) }
Alternatively, add an asList() to the Optional class.
(edit: 2021-09-13, missing bracket in code example)
Attachments
Issue Links
- relates to
-
GROOVY-10616 Add peek extension method for Optional, OptionalInt, OptionalLong and OptionalDouble
- Closed
- links to