gpt4 book ai didi

带有模棱两可关闭的groovy : How to resolve

转载 作者:行者123 更新时间:2023-12-01 10:25:02 26 4
gpt4 key购买 nike

我正在尝试在 groovy 中做到这一点

def results = new File(someDirectory).listFiles { it.name.startsWith 'foo'}

但是,我得到了可以理解的错误:
groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method java.io.File#listFiles.
Cannot resolve which method to invoke for [class change$_getRollback_closure1] due to overlapping prototypes between:
[interface java.io.FileFilter]
[interface java.io.FilenameFilter]

我如何告诉它使用带有 FileFilter 的那个?

我最终写了它普通的 Java 风格:
File[] files = someDir.listFiles( new FileFilter(){
@Override
boolean accept(File pathname) {
return pathname.name.startsWith("foo")
}
})

最佳答案

您可以强制转换或强制闭包以指定您所指的 SAM 类型/功能接口(interface)。

// using a typecast:
def results = new File(someDirectory).listFiles((FileFilter) { it.name.startsWith 'foo'})

// using as keyword:
def results = new File(someDirectory).listFiles({ it.name.startsWith 'foo'} as FileFilter)

// using SAM-type coercion:
FileFilter ff = { it.name.startsWith 'foo'}
def results = new File(someDirectory).listFiles(ff)

关于带有模棱两可关闭的groovy : How to resolve,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48120178/

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