You are currently viewing Unsigned literals : convenience idea

Unsigned literals : convenience idea

So, i recently started a project : a NES emulator in Kotlin.

Obviously, the use of UByte and UShort is really necessary to implement and emulate the behavior of the CPU for example, and as a matter of fact this is actually the first time i really have to use those types (I use UInt often though).

That made me realize a very big problem with how literals are written in Kotlin :

0u

this is a UInt, fine. If i declare a value as UByte and immediately assign it

0u

, the compiler will be able to auto cast it (as it is in bounds) ->

val ubyte: UByte = 0u

.

However, this autocast does not occur outside of assignments or arithmetic operation : when comparing with == for example, i HAVE TO cast it.

if (ubyte == 0u.toUByte())

-> this will not compile without the cast. And worse, in an assertEquals, this will compile, but will not pass the test.

This is incredibly infuriating when having to do that every single time, and a lot.

So I’d think it would be nice to have additional literal identifiers like ub and us for unsigned byte and unsigned short respectively.

0ub

,

0us

-> we wouldn’t need to cast that, it’s explicit.

What do you think about that ?

submitted by /u/n0tKamui
[link] [comments]