将Java Lambda转换为Kotlin Lamba时出错

时间:2018-10-29 04:31:50

标签: android lambda kotlin

我想使用Fetch2在我的应用程序中下载文件,但尝试时出现此错误。

Java中的示例代码:[来自This link]

    fetch.enqueue(request, updatedRequest -> {
        //Request was successfully enqueued for download.
    }, error -> {
        //An error occurred enqueuing the request.
    });

我的代码[Kotlin]。

    fetch.enqueue(request,
    success = { _: com.tonyodev.fetch2.Request ->
        TODO()        
    },
    failed = {  _: com.tonyodev.fetch2.Error ->
        TODO()
    })

这是我得到的错误: Image

编辑: 编译代码时出现此错误。

None of the following functions can be called with the arguments supplied:
public abstract fun enqueue(request: Request, func: Func<Request>? = ..., func2: Func<Error>? = ...): Fetch defined in com.tonyodev.fetch2.Fetch
public abstract fun enqueue(requests: List<Request>, func: Func<List<Request>>? = ..., func2: Func<Error>? = ...): Fetch defined in com.tonyodev.fetch2.Fetch

5 个答案:

答案 0 :(得分:1)

尝试

  fetch.enqueue(request,
    success = { 
        TODO()        
    },
    failed = { 
        TODO()
    })

希望获得帮助

答案 1 :(得分:1)

您是否已从要在此处使用的方法中检查了签名和javadoc?

/**
     * Queues a request for downloading. If Fetch fails to enqueue the request,
     * func2 will be called with the error.
     * Errors that may cause Fetch to fail the enqueue are :
     * 1. No storage space on the device.
     * 2. Fetch is already managing the same request. This means that a request with the same url
     * and file name is already managed.
     * @param request Download Request
     * @param func Callback that the enqueued request will be returned on.
     *             Fetch may update a request depending on the initial request's Enqueue Action.
     *             Update old request references with this request.
     * @param func2 Callback that is called when enqueuing a request fails. An error is returned.
     * @throws FetchException if this instance of Fetch has been closed.
     * @return Instance
     * */
    fun enqueue(request: Request, func: Func<Request>? = null, func2: Func<Error>? = null): Fetch

/**
 * Queues a list of requests for downloading. If Fetch fails to enqueue a
 * download request because an error occurred, all other request in the list will
 * fail. Func2 will be called with the error message.
 * Errors that may cause Fetch to fail the enqueue are :
 * 1. No storage space on the device.
 * 2. Fetch is already managing the same request. This means that a request with the same url
 * and file name is already managed.
 * @param requests Request List
 * @param func Callback that the enqueued request will be returned on.
 *             Fetch may update a request depending on the initial request's Enqueue Action.
 *             Update old request references with this request.
 * @param func2 Callback that is called when enqueuing a request fails. An error is returned.
 * @throws FetchException if this instance of Fetch has been closed.
 * @return Instance
 * */
fun enqueue(requests: List<Request>, func: Func<List<Request>>? = null, func2: Func<Error>? = null): Fetch

因此,第二个和第三个参数基本上是回调,您甚至可以跳过或调用诸如fetch.enqueue(request,object:Func {//在这里实现回调方法 },...)

答案 2 :(得分:1)

当前,将Java SAM转换为kotlin lambda时,必须显式指定类型。因此,您的代码应类似于:

fetch.enqueue(request,
    success = Func<Request> { _: com.tonyodev.fetch2.Request ->
        TODO()        
    },
    failed = Func<Error> {  _: com.tonyodev.fetch2.Error ->
        TODO()
    })

答案 3 :(得分:0)

即使我不确定它如何工作,我现在也可以解决我的问题。

    fetch.enqueue(request)
        Func<Request> { _ ->
            //TODO()
        }
        Func<com.tonyodev.fetch2.Error> { _ ->
            //TODO()

    fetch.enqueue(request,
        Func { _ ->
            //TODO()
        },
        Func { _ ->
            //TODO()
        })

答案 4 :(得分:0)

正在运行的最短版本

fetch.enqueue(request, Func {
    // success
}, Func {
   // failure
})