Suggestion: Primitive byte char handling

I think that low level string handling in Kotlin is not very practical.

For instance, if I need remove 0 extra byte in the end, the shortest code is.

...removeSuffix(0.toChar().toString()...

I use this code because I’ve found a intermitent bug in reading some long text types in SQLite, that
which forces me to read the field as blob. In that case it returns to me an extra byte equal to 0.

            val conteudo =String(c.getBlob(
                c.getColumnIndex("Conteudo"))).removeSuffix(0.toChar().toString())

For adding some specific byte in a string, the code is.

var s:String
....
s = s + 0.toChar()

Code s = s + /u0000 is a right solution in Java, but not in Kotlin:

class Main {
  public static void main(String[] args) {
    String s = "whale\u0000";
    Integer cod = (int) s.charAt(5);
    System.out.println(cod);
  }
}

It would be nice if extend escape \u usage for include prefix \u00.. like Java or create a new escape \c for enter a byte character. It’s also useful for network protocols.

For instance \c0B it would be a Vertical TAB character (#11) and \c00 it would be a #00 (Null character)

I could write in Kotlin:

var s = "Whale\c00"

It also would be useful a B suffix for Byte type. It is ugly compares an Integer with a Byte in Kotlin:

var a:Byte = 10
....
if (a==10.toByte()) 

It would be a nice syntax sugar

var a:Byte = 10
...
 if (a==10B)

You can use \u0000 char literal in Kotlin as well:

fun main() {
    val text = "Whale\u0000"
    println(text.removeSuffix("\u0000").length)
}

Yes,@ilya.gorbunov, you are right. I’m sorry for that. I’ve tested yesterday, maybe I was a little bit tired. So it is solved! However, B suffix in byte literal is not a bad idea…

Unfortunately, B suffix can be misinterpreted as hex digit, e.g. what is 0x1B: is it a Byte with value 1 or an Int with value 27?

We know about the inconvenience of using integer literals after == and are discussing it here: https://youtrack.jetbrains.com/issue/KT-3936

Thanks for your feedback and link suggestion, @ilya.gorbunov

I don’t know how the compiler heuristic works but I think that the 0x preffix shoud have precedence over the suffix B,

So 0x2B is hexa 2Aand 2B is the byte 2

It’s the same situation about F suffix. F is a valid hexadecimal digit, but it is a valid suffix that indicates Float type.

fun main() {
    val s = 2F
    val b = 0x2F.toFloat()
    println("$s and $b")  // There is no confusion!
}

So I also think that D should be be a valid suffix for Double type

var d:Double
if (d==2D)

The alternative is ugly

var d:Double
if (d==2.toDouble())

Update (thanks, @nbilyk) One can write,

       if (d==2.0)

But for Byte type there is no good solution

         var d:Byte
         if (d==2.toByte())

Between the confusion of disregarding the prefix 0x and one make some confusion between 0x2B and 2B and the lack of isonomy of not accepting ALL numerical types as possible suffixes, I take the first option.

Isn’t 2.0 the double literal?

I get what you’re saying though, all primitive types should have a literal way of expressing them. Double, Float, Int, Char, String, and Long all have them, but not Byte?

Yes, @nbilyk It’s what I’m saying. About Double, you are right. I’m forgeting it… Yes, 2.0 is a Double literal, but it’s not explicitly what a person with little knowledge of Kotlin would interpret. So if all primitive number type has a suffix, why not double. String and Char type suffix is not necessary because ’ and ‘’ delimiter play this role. I prefer make programs for people that has no deep language understanding, including me, maybe, some years later.

1 Like