Alexei
Alexei

Reputation: 15674

Can't call kotlin function with varargs from java

I need to pass two parameters as varargs (typesList and operationStatus). Here code:

fun getOperationsList(
        vararg typesList: OperationType,
        operationStatus: OperationStatus,
        from: Date, to: Date,
        callback: Callback<List<Operation>>
    ) {

I need call this from java code:

Here java code:

Date[] selectedPeriod = getSelectedPeriod();
        TransportService.INSTANCE.getOperationsList(new OperationType[]{OperationType.PAYMENT, OperationType.PAYOUT}
                , new OperationStatus[]{OperationStatus.EXECUTED, OperationStatus.CREATED}, selectedPeriod[0], selectedPeriod[1],

But I get compile error:

Wrong 2nd argument type. Found: 'OperationStatus[]', required: 'OperationStat

Upvotes: 0

Views: 489

Answers (2)

Demigod
Demigod

Reputation: 5635

I need to pass two parameters as vararg

It's not possible. According to Kotlin documentation

Only one parameter may be marked as vararg

Also, vararg parameter should be the last one in the function declaration. Or addressed as a named parameter (by name).

If a vararg parameter is not the last one in the list, values for the following parameters can be passed using the named argument syntax

Upvotes: 4

Ravi Goshai
Ravi Goshai

Reputation: 1

instead of passing array of OperationStatus try passing object of OperationStatus

Upvotes: -2

Related Questions