[slf4j-dev] Lambda support in SLF4J
Ceki Gülcü
ceki at qos.ch
Thu Dec 22 15:52:12 CET 2016
Hello all,
Given that JDK 7 earlier are used by over 50% of Java projects, I do not
think we can force users to upgrade to Java 8. Thus, SLF4J is unlikely
to bump its JDK requirement to version 8 for yet some time.
On the bright side, there is a way for org.slf4j.Logger to support Java8
lambdas, or just lambdas, without requiring Java 8. As stated in [1],
the compiler will treat any interface meeting the definition of a
functional interface as a functional interface regardless of whether or
not a @FunctionalInterface annotation is present on the interface
declaration.
This means that we can create a Supplier interface such as
public interface Supplier<T> {
T get();
}
and then add the following method to org.slf4j.Logger interface.
package org.slf4j;
public interface Logger {
....
public void debug(Supplier<T> supplier);
....
}
This will add lambda support without requiring Java 8 support at build time.
The downside of this approach is that implementations of the
org.slf4j.Logger interface as found in logback and modules such as
slf4j-simple, slf4j-log412, slf4j-jdk14 will need to be updated. Given
that the number of such implementations is limited, I think the
requirement is quite acceptable.
Your comments are welcome,
--
Ceki Gülcü
[1]
https://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html
More information about the slf4j-dev
mailing list