-1

I would like to combine these two function but I'm on trouble how to do it. Could anyone help me please?

fun String?.pluralize(count: Int): String? {
    return if (count != 1) {
        this + 's'
    } else {
        this
    }
}

fun String?.pluralize(count: Int, plural: String?): String? {
    return if (count != 1) {
        plural ?: this + 's'
    } else {
        this
    }
}
Stefano Sansone
  • 2,377
  • 7
  • 20
  • 39
John
  • 17
  • 1
  • 8
  • What is the result that you want? – Sweeper Sep 09 '21 at 08:03
  • 2
    not sure what you are expecting but `plural: String? = null ` ? – sidgate Sep 09 '21 at 08:09
  • Also note that your function does not work as you probably expect when input string is null. `null.pluralize(2)` returns `"nulls"` string, but I guess it should return `null`. – broot Sep 09 '21 at 08:36
  • I'm sorry for the unclear question. What I'm trying to do is that I would like to combine the two functions. When you use ``.pluralize`` with one parameter, it returns plural form with 's' if the value is not 1. When you add two parameter, it returns like ''person'' to ''people'' if the value is not 1. – John Sep 09 '21 at 08:46
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 15 '21 at 12:09

3 Answers3

0

The correct way to get the desired result and handle the null case of the input String should be the following:

fun String?.pluralize(count: Int, plural: String? = null): String? = when {
    count == 1 || this == null -> this
    else -> plural ?: "${this}s"
}
0

The real problem here is that you have a special requirement for this code and you did not mention this in your question. This is why all answers you get don't work for you. This additional requirement is that you need to use this function not only from Kotlin, but from Java and/or some reflection-based framework. And that changes a lot.

Regular answer to your question is that you need to just use your second function, but declare its parameter as: plural: String? = null. This is enough to use it as you expect in Kotlin:

"hour".pluralize(2) // hours
"hour".pluralize(2, "foo") // foo

But from the Java you will then only see a function receiving 3 parameters:

PluralizeExtensionKt.pluralize("hour", 2, null); // hours
PluralizeExtensionKt.pluralize("hour", 2, "foo"); // foo
PluralizeExtensionKt.pluralize("hour", 2); // compile error

To fix this problem, you need to additionally annotate this function with @JvmOverloads. This will generate two functions in the Java bytecode, so all above examples will work as you expect.

The resulting function looks like this:

@JvmOverloads
fun String?.pluralize(count: Int, plural: String? = null): String? {
    return if (count != 1) {
        plural ?: this + 's'
    } else {
        this
    }
}

Still, I suggest using an implementation provided in Михаил Нафталь answer. It fixes an additional bug in your code and is more concise.

broot
  • 21,588
  • 3
  • 30
  • 35
  • Thank you for the answer. I solved the problem adding null as default value and add ```@JvmOverloads``` annotation. It seems like we need that annotation to functions and constructors with default values. Also, the annotation forces it generate multiple overloads. Thank you so much again. This was my first post on Stack overflow and It is amazing to connect with people over the world! – John Sep 10 '21 at 02:57
0

You can add the default value as null in second parameter

fun String?.pluralize(count: Int, plural: String?=null): String? {
    return if (count != 1) {
        plural ?: this + 's'
    } else {
       this
    }
}
Andronicus
  • 25,419
  • 17
  • 47
  • 88
pigoth
  • 11
  • Thank you for your cooperation. I solved the problem adding ```@JvmOverloads``` annotation with this code. – John Sep 10 '21 at 02:59