Error when converting Java Lambda to Kotlin Lamba

Viewed 297

I want to use Fetch2 to download a file in my app but I got this error when I tried.

Sample code in Java: [ From This link ]

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

My code [Kotlin].

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

This is the error which I got: Image

Edit: I got this error when compile my code.

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 Answers

A shortest version which is working

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

try it

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

hope this help

Have you checked the signature and the javadoc from the method you're trying to use here?

/**
     * 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

So the second and third parameters are basically callbacks that you can even skip or do call something like fetch.enqueue(request, object : Func { //implement callback methods here }, ...)

Currently, when converting Java SAMs to kotlin lambdas you have to specify the type explicitly. So, your code should look something like:

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

I can solve my problem now even I'm not sure how it work.

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

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