String.Empty Field

What is the problem with ""? emptyList(), emptyMap are different. There isn’t any other way to get what they provide since they are backed by a special implementation of List, Map also there isn’t a literal you can use for them. We don’t use Int.Empty or Int.Zero either.

That all said if you need a function/property for this for any reason feel free to add this to your project. I just don’t see any reason why we need this in the standard library.

1 Like

Maybe slightly off topic, but why do you need emptyMap()?

This seems to have the same result:
val myEmptyMap = mapOf<Int, Int>()

The problem with “” that you cannot use it with string interpolation, you have to define variable or function. We don’t have Int.Zero but Java provides BigDecimal.Zero, BigDecimal.One, and even BigDecimal.Two. I understand need for Zero and One, but I don’t see any reason for Two, nevertheless someone saw the reason and added it. Why it is that big deal to have field or function to represent empty string?

It doesn’t matter do you need the function emptyMap() or not, it already exists.

I personally prefer to follow declarative style of programming saying what I want to have or to do instead of imperatively defining how it should be done. This is why I prefer to use emptyList() instead of listOf().

Sure you can. Out of the box. Replace emptyString() with “” in your last example. It will work.

1 Like