1

I have a function

fun foo(
    id: String,
    vararg values: Int,
){
  ...    
}

and there are calls like these

fun bar1(){
    foo("id_1")
    foo("id_2", 1)
    foo("id_3", 1, 2, 3)
}

Now I've understood that I need to add one more argument to foo. but I don't want to break existing calls so I tried to add it with default values:

fun foo(
    id: String,
    attributes: Array<String> = arrayOf("a", "b"),
    vararg values: Int,
){
  ...
}

But

foo("id_2", 1)
foo("id_3", 1, 2, 3)

became broken.

Is there way to add one more argument with default value without breaking existing calls ?

2 Answers 2

1

Option 1:

You can, if you move the new parameter to the end:

fun foo(
    id: String,
    vararg values: Int,
    attributes: Array<String> = arrayOf("a", "b"),
) {
    // ...
}

But it becomes a bit awkward to call:

fun bar1() {
    foo("id_1")
    foo("id_2", 1)
    foo("id_3", 1, 2, 3)
    foo("id_3", 1, 2, 3, attributes = arrayOf("a", "b"))
}

You need to specify the parameter name, otherwise the compiler will think the array is still part of the varargs parameter.


Option 2:

You can overload the function:

fun foo(
    id: String,
    vararg values: Int,
) {
    foo(id, arrayOf("a", "b"), *values)
}

fun foo(
    id: String,
    attributes: Array<String>,
    vararg values: Int,
) {
    // ...
}

Then you can call it like this:

fun bar1() {
    foo("id_1")
    foo("id_2", 1)
    foo("id_3", 1, 2, 3)
    foo("id_3", arrayOf("a", "b"), 1, 2, 3)
}

This is more elegant at the call site but you have a little overhead at the declaration side.

Sign up to request clarification or add additional context in comments.

Comments

1

I am new to kotlin but you made me learn this today, thanks! was using kotlin playground..


You were asking about way to add one more argument with default value without breaking existing calls.

You can do this by using attributes after vararg like

fun foo(
    id: String,
    vararg values: Int,
    attributes: Array<String> = arrayOf("a", "b"),
){
  ...
}

But wait, that will raise error regarding type, because the attribute which you meant to convey at last of foo() will also be considered as vararg, so to use this code we will have to call the function like:

foo("id_3", 1, 2, 3,attributes=loremipsum)

wherever required you can add ,attributes=loremipsum and if not leave it.

1 Comment

so there is not nice way to do it....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.