ajwang / groovypptest

Automatically exported from code.google.com/p/groovypptest
0 stars 0 forks source link

Operator + type inference fails with Set #357

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
 Compile the following expression in @Typed code:
    Collections.unmodifiableSet(new HashSet() + 1)

 Its compilation produces: "Cannot find static method Collections.unmodifiableSet(Collection<Integer>)"

What is the expected output? What do you see instead?
 I expect no error. Groovy++ should infer type Set for "new HashSet() + 1". 
 In Groovy, the return type for + seems to be governed by the first argument.

 I unfortunately get "Cannot find static method Collections.unmodifiableSet(Collection<Integer>)"

What version of the product are you using? On what operating system?
 Groovy Version: 1.7.8 JVM: 1.7.0-ea
 groovypp-0.4.170.jar

Original issue reported on code.google.com by manu....@gmail.com on 22 Mar 2011 at 12:07

GoogleCodeExporter commented 8 years ago
This is not a bug.

If you look on following definition from Groovy you will find that typeof(new 
HashSet() + 1) == Collection<Integer> 

public static <T> Collection<T> plus(Collection<T> left, T right) {
  //...
}

so method

    public static <T> Set<T> unmodifiableSet(Set<? extends T> s) {

is not applicable.

Original comment by alex.tka...@gmail.com on 22 Mar 2011 at 6:16

GoogleCodeExporter commented 8 years ago
If you don't want to call it a bug, call it a request for feature.
However as long a the code for plus uses cloneSimilarCollection, the compiler 
can assert that the return type of plus is the same as the first parameter, and 
it is worth adding a type cast after the method call.

Appendix:

    public static <T> Collection<T> plus(Collection<T> left, Collection<T> right) {
        final Collection<T> answer = cloneSimilarCollection(left, left.size() + right.size());
        answer.addAll(right);
        return answer;
    }

Original comment by manu....@gmail.com on 23 Mar 2011 at 9:33