Search code examples
javaarraysfunctional-interface

Creating an empty array of functional interfaces


I have a method which returns an empty array of Function<T,V>

public <T, V> Function<T, V>[] foo() {
    ...
    return (Function<T, V>[]) new Object[0];            
    ...
}

But I catch java.lang.ClassCastException at runtime. Is it possible to return an empty array of functional interfaces?


Solution

  • The closest thing you can do is

    return (Function<T, V>[]) new Function[0];
    

    This has nothing to do with functional interfaces, and everything to do with arrays and generics not interacting well.