我的项目之一是抛出 lambda 表达式 https://github.com/fge/throwing-lambdas;我的目标是简化潜在的使用@FunctionalInterface
s in Stream
s,其在流中使用的唯一“缺陷”是它们抛出检查异常(就我而言,我宁愿称其为缺陷,因为您不能从流中抛出检查异常,但这是另一个故事)。
So, for Function<T, R>
我这样定义:
@FunctionalInterface
public interface ThrowingFunction<T, R>
extends Function<T, R>
{
R doApply(T t)
throws Throwable;
default R apply(T t)
{
try {
return doApply(t);
} catch (Error | RuntimeException e) {
throw e;
} catch (Throwable throwable) {
throw new ThrownByLambdaException(throwable);
}
}
}
例如,这允许我定义:
final ThrowingFunction<Path, Path> = Path::toRealPath;
(why Path::toRealPath
... 出色地,正是因为它有省略号 http://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.html#toRealPath-java.nio.file.LinkOption...-).
我不想在这里停下来,我希望能够写出这样的东西:
Throwing.function(Path::toRealPath).fallbackTo(Path::toAbsolutePath)
上面的内容几乎有效...继续阅读。
我也这样定义:
public abstract class Chainer<N, T extends N, C extends Chainer<N, T, C>>
{
protected final T throwing;
protected Chainer(final T throwing)
{
this.throwing = throwing;
}
public abstract C orTryWith(T other);
public abstract <E extends RuntimeException> T orThrow(
final Class<E> exclass);
public abstract N fallbackTo(N fallback);
public final <E extends RuntimeException> T as(final Class<E> exclass)
{
return orThrow(exclass);
}
}
这是它的实现Function
s:
public final class ThrowingFunctionChain<T, R>
extends Chainer<Function<T, R>, ThrowingFunction<T, R>, ThrowingFunctionChain<T, R>>
implements ThrowingFunction<T, R>
{
public ThrowingFunctionChain(final ThrowingFunction<T, R> function)
{
super(function);
}
@Override
public R doApply(final T t)
throws Throwable
{
return throwing.doApply(t);
}
@Override
public ThrowingFunctionChain<T, R> orTryWith(
final ThrowingFunction<T, R> other)
{
final ThrowingFunction<T, R> function = t -> {
try {
return throwing.doApply(t);
} catch (Error | RuntimeException e) {
throw e;
} catch (Throwable ignored) {
return other.doApply(t);
}
};
return new ThrowingFunctionChain<>(function);
}
@Override
public <E extends RuntimeException> ThrowingFunction<T, R> orThrow(
final Class<E> exclass)
{
return t -> {
try {
return throwing.doApply(t);
} catch (Error | RuntimeException e) {
throw e;
} catch (Throwable throwable) {
throw ThrowablesFactory.INSTANCE.get(exclass, throwable);
}
};
}
@Override
public Function<T, R> fallbackTo(final Function<T, R> fallback)
{
return t -> {
try {
return doApply(t);
} catch (Error | RuntimeException e) {
throw e;
} catch (Throwable ignored) {
return fallback.apply(t);
}
};
}
}
到目前为止一切顺利(尽管IDEA无法识别代码orTryWith()有效 https://youtrack.jetbrains.com/issue/IDEA-137179,但那是另一个故事了)。
我还定义了一个名为的实用程序类Throwing
问题在于main()
我作为测试编写的这个类:
public final class Throwing
{
private Throwing()
{
throw new Error("nice try!");
}
public static <T, R> ThrowingFunctionChain<T, R> function(
final ThrowingFunction<T, R> function)
{
return new ThrowingFunctionChain<>(function);
}
public static void main(final String... args)
{
// FAILS TO COMPILE
final Function<Path, Path> f = function(Path::toRealPath)
.fallbackTo(Path::toAbsolutePath);
}
}
现在,上面代码的错误消息是:
Error:(29, 48) java: incompatible types: cannot infer type-variable(s) T,R
(argument mismatch; invalid method reference
method toRealPath in interface java.nio.file.Path cannot be applied to given types
required: java.nio.file.LinkOption[]
found: java.lang.Object
reason: varargs mismatch; java.lang.Object cannot be converted to java.nio.file.LinkOption)
Error:(29, 49) java: invalid method reference
non-static method toRealPath(java.nio.file.LinkOption...) cannot be referenced from a static context
Error:(30, 25) java: invalid method reference
non-static method toAbsolutePath() cannot be referenced from a static context
我无法在这里诊断错误的确切原因,但对我来说,它只是看起来省略号妨碍了;事实上,如果我这样做:
final ThrowingFunctionChain<Path, Path> f = function(Path::toRealPath);
try (
final Stream<Path> stream = Files.list(Paths.get(""));
) {
stream.map(f.fallbackTo(Path::toAbsolutePath))
.forEach(System.out::println);
}
然后它编译:所以这意味着Stream.map()
确实承认结果是Function
...
那么为什么不呢Throwing.function(Path::toRealPath).fallbackTo(Path::toAbsolutePath)
编译?