Standard interface for a function with no arguments

The Java 6 standard interface (or any compatible library) lacks an argument function and generic return type.

Something like:

interface Foo<T> {
      T call();
}

      

+3


source to share


1 answer


This a Supplier<T>

- accepts nothing and supplies T

. And the method abstract

it defines is nice to callget()

interface Supplier<T> {
   T get();
}

      



Note that in Java8 this ( @FunctionalInterface

) already exists (it is called Supplier

), so if you run your code under Java8 there is no need to define a user interface.

Also, if you run your code under some pre-Java8 version, you can use the Guava interface Supplier

.

+4


source







All Articles