I wrote the following code to simulate Lazy<T>
in Java:
import java.util.function.Supplier;
public class Main {
@FunctionalInterface
interface Lazy<T> extends Supplier<T> {
Supplier<T> init();
public default T get() { return init().get(); }
}
static <U> Supplier<U> lazily(Lazy<U> lazy) { return lazy; }
static <T>Supplier<T> value(T value) { return ()->value; }
private static Lazy<Thing> thing = lazily(()->thing=value(new Thing()));
public static void main(String[] args) {
System.out.println("One");
Thing t = thing.get();
System.out.println("Three");
}
static class Thing{ Thing(){System.out.println("Two");}}
}
but I get the following warning:
"value
(T)
in Main cannot be applied to (com.company.Main.Thing
) reason: no instance(s) of type variable(s) T exist so thatSupplier<T>
conforms toLazy<Thing>
"
could you please help me find out what the problem is? thanks in advance!