[ 
https://issues.apache.org/jira/browse/GROOVY-10223?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17414323#comment-17414323
 ] 

Stephen Smith commented on GROOVY-10223:
----------------------------------------

Thanks [~emilles] for the suggestion.

I can't disagree that it's an option, though sub-optimal to rely on implicit 
conversion of a stream → list in Groovy code.

Though I recognize that adding support for another type to 
{{DefaultTypeTransformation.asCollection() }}might change semantics in existing 
code for some users, the number of users intentionally using an Optional 
wrapped in a List as the current implementation supports is likely vanishingly 
small.

> Add support for Optional to DefaultTypeTransformation.asCollection()
> --------------------------------------------------------------------
>
>                 Key: GROOVY-10223
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10223
>             Project: Groovy
>          Issue Type: New Feature
>          Components: groovy-jdk
>            Reporter: Stephen Smith
>            Priority: Trivial
>              Labels: features
>
> 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
> {noformat}
> DefaultTypeTransformation.asCollection(){noformat}
> 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.
> {code:java}
> if (value instanceOf Optional) {
>    return ((Optional)value).map(List::of).orElse(List.of()
> }{code}
>  Alternatively, add an *asList()* to the Optional class.
>  
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to