salmanahmad / silo

The Silo Programming Language
0 stars 0 forks source link

VarArgs Method Resolution Different From Java #81

Open salmanahmad opened 10 years ago

salmanahmad commented 10 years ago

There is a potential source of confusion when you attempt to pass an array into vararg method that accepts a superclass of the input argument array (like passing String[] into a vararg method that accepts Object[]). The question is: (1) should the argument be interpreted as the first element of the varargs or (2) should the argument be the actual vararg list itself.

As an example, this code inserts an entire array as the first element of the vararg rather than the actual vararg list itself.

list : array(String) = array(String, 3)
list(0) = ""
list(1) = ""
list(2) = ""

vector.create(list)

This function returns [ [Ljava/lang/String@12345 ].

Note that Java does the opposite:

public class varargs {
    public static java.util.List foo(Object... x) {
        return java.util.Arrays.asList(x);
    }

    public static void main(String[] args) {
        String[] s = new String[] {"", "", ""};
        System.out.println(foo(s));
    }
}

This returns: ["", "", ""].

But more importantly, Java gives a cool warning:

varargs.java:9: warning: non-varargs call of varargs method with inexact argument type for last parameter;
        System.out.println(foo(s));
                               ^
  cast to Object for a varargs call
  cast to Object[] for a non-varargs call and to suppress this warning
1 warning

I should update Silo so that it is consistent with Java.