[
https://issues.apache.org/jira/browse/GROOVY-7654?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16019021#comment-16019021
]
ASF GitHub Bot commented on GROOVY-7654:
----------------------------------------
Github user blackdrag commented on a diff in the pull request:
https://github.com/apache/groovy/pull/546#discussion_r117645462
--- Diff:
src/test/org/codehaus/groovy/runtime/DefaultGroovyMethodsTest.groovy ---
@@ -236,7 +236,28 @@ public class DefaultGroovyMethodsTest extends
GroovyTestCase {
assertEquals(3, list.get(2));
}
+ // GROOVY-7654
+ public void testIterableAsList() {
+ def list = [1, 2, 3]
+ def iterable = new IterableWrapper(delegate: list)
+
+ def iterableAsList = iterable.asList()
+ def iterableAsType = iterable as List
+
+ assertEquals(iterableAsList, iterableAsType)
+ assertEquals(1, iterableAsList[0])
+ assertEquals(1, iterableAsType[0])
+ }
+
private static class MyList extends ArrayList {
public MyList() {}
}
+
+ private static class IterableWrapper implements Iterable {
+ Iterable delegate
+
+ Iterator iterator() {
+ delegate.iterator()
+ }
+ }
}
--- End diff --
interesting choice to use Collection.class.isAssignableFrom(clazz). But
sure, why not... +1
> Iterable as List and Iterable.asList() have different semantics
> ---------------------------------------------------------------
>
> Key: GROOVY-7654
> URL: https://issues.apache.org/jira/browse/GROOVY-7654
> Project: Groovy
> Issue Type: Bug
> Components: groovy-jdk
> Affects Versions: 2.4.5
> Reporter: Shil Sinha
>
> For an Iterable `foo` which is not also a collection, `foo.asList()` and `foo
> as List` are not equivalent. The latter goes through the asType(Object,
> Class) path and ultimately returns a proxy. This is unexpected, and can
> result in some inconsistencies. A simple example:
> {code}
> class IterableWrapper implements Iterable {
> Iterable delegate
> Iterator iterator() {
> delegate.iterator()
> }
> }
> def itw = new IterableWrapper(delegate: [1,2,3])
> def itwAsList = itw.asList()
> def itwAsTypeList = itw as List
> assert itwAsList == itwAsTypeList
> assert itwAsList[0] == itwAsTypeList[0]
> {code}
> The first assertion passes, but the second fails with:
> {code}
> groovy.lang.MissingMethodException: No signature of method:
> IterableWrapper.get() is applicable for argument types: (java.lang.Integer)
> values: [0]
> {code}
--
This message was sent by Atlassian JIRA
(v6.3.15#6346)