英文:
Java error: incompatible types: no instance(s) of type variable(s) T exist so that Optional<T> conforms to Iterable<AvailableLicence>
问题
通常我是一名C#开发者,但我正在尝试用Java编写一些代码(多年来的第一次),使用泛型来创建一个“Retry”方法,允许我使用lambda表达式来重试任何代码块。这是我的重试方法:
public static <T> Optional<T> runWithRetry(final Supplier<T> t, int retryAttempts, long retryDelayInMilliseconds) throws InterruptedException {
for(int retry = retryAttempts; retry >= 0; retry--) {
try{
return Optional.of(t.get());
}
catch(Exception e) {
if(retry == 0) {
throw e;
} else if (retryDelayInMilliseconds > 0) {
Thread.sleep(retryDelayInMilliseconds);
}
}
}
return Optional.empty();
}
我试图像这样调用这个方法:
Iterable<AvailableLicence> licences = Helpers.runWithRetry(() -> {
return licensor.findAvailableLicences(licenseOptions);
}, 3, 5000);
但Java编译器抛出了这个错误:
error: incompatible types: no instance(s) of type variable(s) T exist so that Optional<T> conforms to Iterable<AvailableLicence>
Iterable<AvailableLicence> licences = Helpers.runWithRetry(() -> {
^
where T is a type-variable:
T extends Object declared in method <T>runWithRetry(Supplier<T>,int,long)
我认为这个错误试图告诉我的是,我没有在某处为<T>
指定类型,但我不是完全确定。我还在结合一些Java的东西,比如Optional
和Supplier
,我不确定它们是否可能导致问题,尽管我已经阅读了它们的工作原理。有人能告诉我在这里我做错了什么吗?我觉得这可能是一个简单的语法问题,我可能遗漏了。
英文:
Normally I'm a C# developer, but I'm trying to write some code in Java (first time in years) that uses generics to create a 'Retry' method that allows me to use a lambda expression to retry any block of code, basically. Here is my retry method:
public static <T> Optional<T> runWithRetry(final Supplier<T> t, int retryAttempts, long retryDelayInMilliseconds) throws InterruptedException {
for(int retry = retryAttempts; retry >= 0; retry--) {
try{
return Optional.of(t.get());
}
catch(Exception e) {
if(retry == 0) {
throw e;
} else if (retryDelayInMilliseconds > 0) {
Thread.sleep(retryDelayInMilliseconds);
}
}
}
return Optional.empty();
}
And I'm attempting to call this method like so:
Iterable<AvailableLicence> licences = Helpers.runWithRetry(() -> {
return licensor.findAvailableLicences(licenseOptions);
}, 3, 5000);
but the Java compiler is throwing this error:
error: incompatible types: no instance(s) of type variable(s) T exist so that Optional<T> conforms to Iterable<AvailableLicence>
Iterable<AvailableLicence> licences = Helpers.runWithRetry(() -> {
^
where T is a type-variable:
T extends Object declared in method <T>runWithRetry(Supplier<T>,int,long)
I think what this error is trying to tell me is that I am not specifying the type for <T>
somewhere but I'm not entirely sure. I'm also combining some Java things like Optional
and Supplier
that I'm not sure if they could be causing the issue or not, despite having read up on how they work. Can anyone tell what I'm doing wrong here? I feel like it's a simple syntax issue I'm missing here.
答案1
得分: 1
以下是翻译好的部分:
我会让 runWithRetry()
的调用者不必处理检查过的 InterruptedException
。 例如像这样…
public static <T> Optional<T> runWithRetry(final Supplier<T> t, int retryAttempts, long retryDelayInMilliseconds) {
return handleSleep(t, retryAttempts, retryDelayInMilliseconds);
}
private static <T> Optional<T> handleSleep(final Supplier<T> t, int retryAttempts, long retryDelayInMilliseconds){
Optional<T> retVal = Optional.empty();
for(int retry = retryAttempts; retry >= 0; retry--) {
try{
retVal = Optional.of(t.get());
}
catch(Exception e) {
if(retry == 0) {
throw e;
} else if (retryDelayInMilliseconds > 0) {
try{
Thread.sleep(retryDelayInMilliseconds);
} catch(InterruptedException ie){ /*TODO: 处理或记录警告 */ }
}
}
}
return retVal;
}
看看我在 我的演示中… 如何使用它:
...
Optional<Iterable<AvailableLicense>> licenses = Deduper.runWithRetry( ()->
{ return licensor.findAvailableLicenses(licenseOptions); }, 3, 5000 );
licenses.ifPresent(out::println);
...
因为我已经实现了 AvailableLicense.toString()
,我的演示输出如下…:
[AvailableLicense[ type: To Kill!]]
Optional - The Mother of all Bikesheds 来自 @StuartMarks — 实现 Optional
的 Oracle 核心开发人员之一,强烈推荐 每个 使用 Optional
的人观看。
英文:
I would spare the caller of runWithRetry()
having to handle the checked InterruptedException
. For example like this…
public static <T> Optional<T> runWithRetry(final Supplier<T> t, int retryAttempts, long retryDelayInMilliseconds) {
return handleSleep(t, retryAttempts, retryDelayInMilliseconds);
}
private static <T> Optional<T> handleSleep(final Supplier<T> t, int retryAttempts, long retryDelayInMilliseconds){
Optional<T> retVal = Optional.empty();
for(int retry = retryAttempts; retry >= 0; retry--) {
try{
retVal = Optional.of(t.get());
}
catch(Exception e) {
if(retry == 0) {
throw e;
} else if (retryDelayInMilliseconds > 0) {
try{
Thread.sleep(retryDelayInMilliseconds);
} catch(InterruptedException ie){ /*TODO: Handle or log warning */ }
}
}
}
return retVal;
}
See how I used that in my demo…
...
Optional<Iterable<AvailableLicense>> licenses = Deduper.runWithRetry( ()->
{ return licensor.findAvailableLicenses(licenseOptions); }, 3, 5000 );
licenses.ifPresent(out::println);
...
Because I've implemented AvailableLicense.toString()
, my demo outputs…
[AvailableLicense[ type: To Kill!]]
Optional - The Mother of all Bikesheds from @StuartMarks — one of the Oracle core devs that implemented Optional
is recommended viewing for everybody using Optional
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论