You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by armsargis <gi...@git.apache.org> on 2017/05/21 19:38:49 UTC

[GitHub] groovy pull request #546: GROOVY-7654: Iterable as List and Iterable.asList(...

GitHub user armsargis opened a pull request:

    https://github.com/apache/groovy/pull/546

    GROOVY-7654: Iterable as List and Iterable.asList() have different semantics

    Introduce DefaultGroovyMethods.asType(Iterable, Class) to handle correctly 'asType' to iterables

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/armsargis/groovy GROOVY-7654

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/groovy/pull/546.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #546
    
----
commit faabd7bd65ef9437add8ce451f84a09066e0b448
Author: Sargis Harutyunyan <sa...@webbfontaine.com>
Date:   2017-05-21T19:35:16Z

    GROOVY-7654: Introduce DefaultGroovyMethods.asType(Iterable, Class) to
    handle correctly 'asType' to iterables

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] groovy pull request #546: GROOVY-7654: Iterable as List and Iterable.asList(...

Posted by blackdrag <gi...@git.apache.org>.
Github user blackdrag commented on a diff in the pull request:

    https://github.com/apache/groovy/pull/546#discussion_r117640763
  
    --- 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 --
    
    I am missing a test 
    
    def iterableAsIterable = iterable as Iterable
    assert iterable.AsIterable.is(iterable)
    def iterableAsIterableWrapper = iterable as IterableWrapper
    assert iterable.AsIterable.is(iterable)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] groovy pull request #546: GROOVY-7654: Iterable as List and Iterable.asList(...

Posted by armsargis <gi...@git.apache.org>.
Github user armsargis commented on a diff in the pull request:

    https://github.com/apache/groovy/pull/546#discussion_r117642093
  
    --- 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 --
    
    Actually, test I got from jira issue and meaning I think is to create concrete implementation of Iterable which is not extending from collection. And for concrete implementation we need return some iterator.
    
    Also I thinking to change code like:
    
    ```
    public static <T> T asType(Iterable iterable, Class<T> clazz) {
            if (Collections.class.isAssignableFrom(clazz)) {
                return asType((Collection) toList(iterable), clazz);
            }
    
            return asType((Object) toList(iterable), clazz);
        }
    ```



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] groovy pull request #546: GROOVY-7654: Iterable as List and Iterable.asList(...

Posted by armsargis <gi...@git.apache.org>.
Github user armsargis commented on a diff in the pull request:

    https://github.com/apache/groovy/pull/546#discussion_r117642241
  
    --- 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 --
    
    I will add missing test cases


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] groovy pull request #546: GROOVY-7654: Iterable as List and Iterable.asList(...

Posted by blackdrag <gi...@git.apache.org>.
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


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] groovy pull request #546: GROOVY-7654: Iterable as List and Iterable.asList(...

Posted by blackdrag <gi...@git.apache.org>.
Github user blackdrag commented on a diff in the pull request:

    https://github.com/apache/groovy/pull/546#discussion_r117645390
  
    --- 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 --
    
    If the iterable is an instance of Collection, then why isnĀ“t the Collection taking method called directly?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] groovy pull request #546: GROOVY-7654: Iterable as List and Iterable.asList(...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/groovy/pull/546


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---