[jira] [Commented] (GROOVY-10223) Add support for Optional to DefaultTypeTransformation.asCollection()
Title: Message Title ASF GitHub Bot commented on GROOVY-10223 Re: Add support for Optional to DefaultTypeTransformation.asCollection() paulk-asert commented on PR #1732: URL: https://github.com/apache/groovy/pull/1732#issuecomment-1167957937 > @paulk-asert I agree, it seems the extension method `Optional collect(Optional,Closure)` is not that valuable. If deprecated, it would not resolve as an extension, which would then cause `List collect(Object,Closure)` to be selected instead. I doubt any code that makes use of this is prepared for a list return value. Yes, it would be a breaking change. We'd deprecate it now in Groovy 4 and remove in Groovy 5 with explanation in release notes. Folks wanting to prepare could potentially test with -Dgroovy.extension.disable=collect but that isn't very selective. We can explain the behavior either way, I just think the explanation is much simpler if it behaves like all the other fallback variants. Add Comment This message was sent by Atlassian Jira (v8.20.10#820010-sha1:ace47f9)
[jira] [Commented] (GROOVY-10223) Add support for Optional to DefaultTypeTransformation.asCollection()
[ https://issues.apache.org/jira/browse/GROOVY-10223?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17559304#comment-17559304 ] ASF GitHub Bot commented on GROOVY-10223: - eric-milles merged PR #1732: URL: https://github.com/apache/groovy/pull/1732 > Add support for Optional to DefaultTypeTransformation.asCollection() > > > Key: GROOVY-10223 > URL: https://issues.apache.org/jira/browse/GROOVY-10223 > Project: Groovy > Issue Type: Improvement > Components: groovy-jdk >Reporter: Stephen Smith >Assignee: Eric Milles >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. > (edit: 2021-09-13, missing bracket in code example) > -- This message was sent by Atlassian Jira (v8.20.7#820007)
[jira] [Commented] (GROOVY-10223) Add support for Optional to DefaultTypeTransformation.asCollection()
[ https://issues.apache.org/jira/browse/GROOVY-10223?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17559303#comment-17559303 ] ASF GitHub Bot commented on GROOVY-10223: - eric-milles commented on PR #1732: URL: https://github.com/apache/groovy/pull/1732#issuecomment-1167664770 @paulk-asert I agree, it seems the extension method `Optional collect(Optional,Closure)` is not that valuable. If deprecated, it would not resolve as an extension, which would then cause `List collect(Object,Closure)` to be selected instead. I doubt any code that makes use of this is prepared for a list return value. > Add support for Optional to DefaultTypeTransformation.asCollection() > > > Key: GROOVY-10223 > URL: https://issues.apache.org/jira/browse/GROOVY-10223 > Project: Groovy > Issue Type: Improvement > Components: groovy-jdk >Reporter: Stephen Smith >Assignee: Eric Milles >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. > (edit: 2021-09-13, missing bracket in code example) > -- This message was sent by Atlassian Jira (v8.20.7#820007)
[jira] [Commented] (GROOVY-10223) Add support for Optional to DefaultTypeTransformation.asCollection()
[ https://issues.apache.org/jira/browse/GROOVY-10223?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17558635#comment-17558635 ] Eric Milles commented on GROOVY-10223: -- Added {{getAt(int)}} so you can do this instead of {{opt*.length()?[0]}} or {{opt.orElse(null)?.length()}} or {{opt.map(String::length).orElse(null)}}: {code:groovy} def opt = Optional.of("foo") def len = opt[0]?.length() assert len == 3 {code} https://github.com/apache/groovy/commit/a0de382914b3c6a1e777c4607e8b7edd1e70608e > Add support for Optional to DefaultTypeTransformation.asCollection() > > > Key: GROOVY-10223 > URL: https://issues.apache.org/jira/browse/GROOVY-10223 > Project: Groovy > Issue Type: Improvement > Components: groovy-jdk >Reporter: Stephen Smith >Assignee: Eric Milles >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. > (edit: 2021-09-13, missing bracket in code example) > -- This message was sent by Atlassian Jira (v8.20.7#820007)
[jira] [Commented] (GROOVY-10223) Add support for Optional to DefaultTypeTransformation.asCollection()
[ https://issues.apache.org/jira/browse/GROOVY-10223?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17558600#comment-17558600 ] Stephen Smith commented on GROOVY-10223: That's awesome [~emilles] > 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 >Assignee: Eric Milles >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. > (edit: 2021-09-13, missing bracket in code example) > -- This message was sent by Atlassian Jira (v8.20.7#820007)
[jira] [Commented] (GROOVY-10223) Add support for Optional to DefaultTypeTransformation.asCollection()
[ https://issues.apache.org/jira/browse/GROOVY-10223?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17558566#comment-17558566 ] ASF GitHub Bot commented on GROOVY-10223: - eric-milles commented on PR #1732: URL: https://github.com/apache/groovy/pull/1732#issuecomment-1165725099 And this kind of thing is not supported without `getAt(int)` or `getAt(Integer)`: ```groovy def opt = Optional.of('') def (String x) = opt assert x == '' ``` > 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 >Assignee: Eric Milles >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. > (edit: 2021-09-13, missing bracket in code example) > -- This message was sent by Atlassian Jira (v8.20.7#820007)
[jira] [Commented] (GROOVY-10223) Add support for Optional to DefaultTypeTransformation.asCollection()
[ https://issues.apache.org/jira/browse/GROOVY-10223?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17558556#comment-17558556 ] ASF GitHub Bot commented on GROOVY-10223: - eric-milles commented on PR #1732: URL: https://github.com/apache/groovy/pull/1732#issuecomment-1165715088 You can also do this, but I don't think it is an improvement over "optional.map(String::length).orElse(null)" or "optional.orElse(null)?.length()": ```groovy def nothing = Optional.empty(), something = Optional.of('foo') def value = nothing*.length()?[0] assert value == null value = something*.length()?[0] assert value == 3 ``` > 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 >Assignee: Eric Milles >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. > (edit: 2021-09-13, missing bracket in code example) > -- This message was sent by Atlassian Jira (v8.20.7#820007)
[jira] [Commented] (GROOVY-10223) Add support for Optional to DefaultTypeTransformation.asCollection()
[ https://issues.apache.org/jira/browse/GROOVY-10223?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17558545#comment-17558545 ] ASF GitHub Bot commented on GROOVY-10223: - eric-milles opened a new pull request, #1732: URL: https://github.com/apache/groovy/pull/1732 Adds support for assignment of optional to arrays or collections > 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 >Assignee: Eric Milles >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. > (edit: 2021-09-13, missing bracket in code example) > -- This message was sent by Atlassian Jira (v8.20.7#820007)
[jira] [Commented] (GROOVY-10223) Add support for Optional to DefaultTypeTransformation.asCollection()
[ https://issues.apache.org/jira/browse/GROOVY-10223?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17558521#comment-17558521 ] Eric Milles commented on GROOVY-10223: -- Since the "iterator()" for Object delegates to {{DefaultTypeTransformation#asCollection}} an addition there would have a side benefit of iterator support. {code:java} public static Iterator iterator(Object o) { return DefaultTypeTransformation.asCollection(o).iterator(); } {code} I'm just looking now ti see what uses {{DefaultTypeTransformation#asCollection}} as I don't see it as part of {{castToType}}. > 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 >Assignee: Eric Milles >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. > (edit: 2021-09-13, missing bracket in code example) > -- This message was sent by Atlassian Jira (v8.20.7#820007)
[jira] [Commented] (GROOVY-10223) Add support for Optional to DefaultTypeTransformation.asCollection()
[ https://issues.apache.org/jira/browse/GROOVY-10223?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17558504#comment-17558504 ] Eric Milles commented on GROOVY-10223: -- Here is my "iterator()" implementation, in case someone wants to try this out as a category method or whatever. I do not see enough value to pursue it plus {{OptionalInt}}, {{OptionalLong}} and {{OptionalDouble}} variants. {code:java} /** * If a value is present in the {@code Optional}, returns a single-element * {@code Iterator}; otherwise returns an empty {@code Iterator}. * * * def iter = Optional.empty().iterator() * assert !iter.hasNext() * * iter = Optional.of('x').iterator() * assert iter.hasNext() * assert iter.next() == 'x' * assert !iter.hasNext() * * // for-each supported via iterator() * int values = 0 * for (value in Optional.empty()) { * values += 1 * } * assert values == 0 * for (value in Optional.of('x')) { * assert value == 'x' * values += 1 * } * assert values == 1 * */ public static Iterator iterator(final Optional self) { return self.map(Collections::singleton).orElseGet(Collections::emptySet).iterator(); } {code} > 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 >Assignee: Eric Milles >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. > (edit: 2021-09-13, missing bracket in code example) > -- This message was sent by Atlassian Jira (v8.20.7#820007)
[jira] [Commented] (GROOVY-10223) Add support for Optional to DefaultTypeTransformation.asCollection()
[ https://issues.apache.org/jira/browse/GROOVY-10223?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17538961#comment-17538961 ] Eric Milles commented on GROOVY-10223: -- The addition of an "iterator()" method would enhance usage in for loop and spread argument: {code:groovy} void ifIteratorProvided(Optional opt) { for (x in opt) { print x // only fires if present } m(*opt) // calls "m()" if absent or "m(param)" if present def list = [*opt] // empty if absent, single value if present } {code} > 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. > (edit: 2021-09-13, missing bracket in code example) > -- This message was sent by Atlassian Jira (v8.20.7#820007)
[jira] [Commented] (GROOVY-10223) Add support for Optional to DefaultTypeTransformation.asCollection()
[ https://issues.apache.org/jira/browse/GROOVY-10223?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17423975#comment-17423975 ] Eric Milles commented on GROOVY-10223: -- {{Optional#ifPresent}} gives you the same support as Scala's {{Option#forEach}}. If you want to get away from {{Optional}} there is {{get()}}, {{orElse(T)}}, {{orElseGet(Supplier)}} or {{stream()}}. Your any example could be expressed as {{optional.filter\{ it > 2 };}}. Your process example could be expressed as {{optional.map\{ process(it) }.ifPresent(processedList::add);}}. Instead of the dual {{List#of}} example from the top, you could make use of the {{toList(Stream)}} extension method to shorten inline list creation if you didn't want to assign to a variable: {{optional.stream().toList()}}. > 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. > (edit: 2021-09-13, missing bracket in code example) > -- This message was sent by Atlassian Jira (v8.3.4#803005)
[jira] [Commented] (GROOVY-10223) Add support for Optional to DefaultTypeTransformation.asCollection()
[ https://issues.apache.org/jira/browse/GROOVY-10223?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17422438#comment-17422438 ] Paul King commented on GROOVY-10223: There is some existing functionality. Groovy truth is already catered for: {code} assert !Optional.empty() assert Optional.of('something') {code} And there is an existing {{collect}} and a {{filter}} "by class" which retain the {{Optional}} nature of the original {{Optional}}. {code} assert Optional.of("something").collect{ it.size() }.get() == 9 assert !Optional.empty().collect{ it.size() }.isPresent() // isPresent() is optional since that is what Groovy truth would use anyway assert !Optional.empty().filter(Number) // filter returns an optional assert !Optional.of('x').filter(Number) assert Optional.of(1234).filter(Number) assert Optional.of(1234).filter(Number).get().equals(1234) {code} But you are correct that the complete list of possibilities isn't covered. Perhaps a good way forward is to add more explicit {{Optional}} handling variants as needed and also the {{asList()}} that you suggested which would covered anyone wanting to jump out of the {{Optional}} nature. > 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. > (edit: 2021-09-13, missing bracket in code example) > -- This message was sent by Atlassian Jira (v8.3.4#803005)
[jira] [Commented] (GROOVY-10223) Add support for Optional to DefaultTypeTransformation.asCollection()
[ https://issues.apache.org/jira/browse/GROOVY-10223?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17422401#comment-17422401 ] Stephen Smith commented on GROOVY-10223: My perception of the situation in Java is that Optional is meant to be important for both noting the existence of the wrapped value and the absence of the value. It's an alternative for returning {{null}} from a method where {{null}} contains information regarding the absence of returned information. Optional can also be used as a carrier of information that's only important if it exists. {code:groovy} def isBiggerThanTwo = optional.any { it > 2 } {code} or you want to process the information if it exists and collect the results, but an empty list is perfectly valid as a result: {code:groovy} def processedList = [ ... ] processedList += optional.collect { process( it ) } {code} It opens up all of the list processing abilities of the language to something that should have been iterable to begin with (and as Java has added {{.stream()}} to Optional, they recognize the value of using it for mapping and filtering) It also opens up the possibility of using Groovy Truth on an Optional, because if it can be converted to a collection, then an empty collection indicates false: {code:groovy} if (optional) { // do something } {code} There are many reasons why languages like Scala prefer viewing Option as an iterable rather than as an value subject to imperative evaluation: [https://stackoverflow.com/questions/6819373/why-is-foreach-better-than-get-for-scala-options/6820001|http://example.com/] [http://blog.tmorris.net/posts/scalaoption-cheat-sheet/|http://example.com/] > 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. > (edit: 2021-09-13, missing bracket in code example) > -- This message was sent by Atlassian Jira (v8.3.4#803005)
[jira] [Commented] (GROOVY-10223) Add support for Optional to DefaultTypeTransformation.asCollection()
[ https://issues.apache.org/jira/browse/GROOVY-10223?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17421972#comment-17421972 ] Paul King commented on GROOVY-10223: [~stephenns] Can you give some more examples of how you would use such functionality? > 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. > (edit: 2021-09-13, missing bracket in code example) > -- This message was sent by Atlassian Jira (v8.3.4#803005)
[jira] [Commented] (GROOVY-10223) Add support for Optional to DefaultTypeTransformation.asCollection()
[ https://issues.apache.org/jira/browse/GROOVY-10223?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=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)
[jira] [Commented] (GROOVY-10223) Add support for Optional to DefaultTypeTransformation.asCollection()
[ https://issues.apache.org/jira/browse/GROOVY-10223?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17414234#comment-17414234 ] Eric Milles commented on GROOVY-10223: -- Support for {{Stream}} converted to array or collection on assignment was added to Groovy 4, so you could do this: {code:groovy} Object[] array = ((Optional) value).stream() List list = ((Optional) value).stream() {code} > 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)