gpt4 book ai didi

java - 在这种情况下,我如何处理 Function 和省略号/可变参数?

转载 作者:搜寻专家 更新时间:2023-11-01 01:42:26 24 4
gpt4 key购买 nike

我的一个项目是 throwing-lambdas ;在其中,我旨在简化潜在的使用 @FunctionalInterfaceStream s,其在流中使用的唯一“缺陷”是它们会抛出已检查的异常(就我而言,我宁愿将无法从流中抛出已检查的异常这一事实称为缺陷,但这是另一回事)。

因此,对于 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;

(为什么 Path::toRealPath ... 嗯,precisely because it has an ellipsis )。

不想在这里停下来,我希望能够写出如下内容:

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 的实现小号:

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 fails to recognize the code of orTryWith() as valid ,但那是另一回事了)。

我还定义了一个名为 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)编译?

最佳答案

你的代码片段

Function<Path, Path> f = function(Path::toRealPath).fallbackTo(Path::toAbsolutePath);

遇到了规范中包含的 Java 8 类型推断的限制,因此这不是编译器错误。当您链接方法调用时,目标类型不起作用。由于链中的第一个方法是可变参数方法,因此需要其目标类型才能找到预期的调用签名。这种情况类似于您编写 p->p.toRealPath() 时,其中调用的参数数量是明确的,但 p 的类型是未知的。两者都不会在调用链中工作(除了最后一次调用)

这可以通过将第一次调用的类型显式化来解决,

Function<Path, Path> f = Throwing.<Path,Path>function(Path::toRealPath)
.fallbackTo(Path::toAbsolutePath);

ThrowingFunctionChain<Path, Path> f0 = function(Path::toRealPath);
Function<Path, Path> f = f0.fallbackTo(Path::toAbsolutePath);

Function<Path, Path> f = function((Path p)->p.toRealPath())
.fallbackTo(Path::toAbsolutePath);

或者通过将方法调用链转换为未链接的方法调用,如described here :

public static <T, R> ThrowingFunctionChain<T, R> function(
final ThrowingFunction<T, R> function)
{
return new ThrowingFunctionChain<>(function);
}
public static <T, R> Function<T, R> function(
final ThrowingFunction<T, R> function, Function<T, R> fallBack)
{
return new ThrowingFunctionChain<>(function).fallbackTo(fallBack);
}
public static void main(final String... args)
{
Function<Path, Path> f = function(Path::toRealPath, Path::toAbsolutePath);
}

当一个调用以另一个调用的结果为目标时,规范故意拒绝对两个调用进行类型推断,但如果相同的表达式只是另一个调用的参数,它就可以工作。

关于java - 在这种情况下,我如何处理 Function<T, R> 和省略号/可变参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28821674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com