Experimental unsigned values

I am trying to experiment if I can get unsigned in a more confortable way…

I followed all the instructions to build Kotlin from source

then I modified

\core\builtins\native\kotlin\Primitives.kt

and removed the private from the declaration

public class Byte () : Number, Comparable<Byte> {

I also removed it from

\generators\src\builtins\primitives.kt

Ps: what’s the difference between these two files exactly?

And then I built. Therefore I substitued all the jars under

$HOME\.IdeaIC2016.2\config\plugins\Kotlin

with the ones I got from the build

I wrote:

fun main(args: Array<String>) {

    class Test : Int() {

    }
}

But I get

Error:(26, 18) Kotlin: This type is final, so it cannot be inherited from
Error:(26, 18) Kotlin: Cannot access ‘< init>’: it is private in ‘Int’

Is there a way I can solve this?

How/when/where will be actually declared the operator overloads of the primitives?

Primitive types are compiled to JVM primitive values, and the operators on primitive types are compiled to direct invocations of the corresponding JVM bytecodes.The JVM does not support unsigned primitive types. You could solve this if you wanted to invest multiple months of your time in learning the details of the Kotlin compiler internals, the JVM implementation and working together with the Kotlin team to design and implement this feature.

Thanks for the explanation, yole

I wish I had the time, because I’d be interesting to learn all those stuff

However for the moment what I would like to have is another class, let’s say Uint, that acts exactly as a normal int, but with some of its operator overloaded, such as the division.

Will it work if I declare it in the same file Primitives.kt and overwrite/declare only those operators?

No, this will not work. Once again: implementing something like this is a multiple-month project.

Uhm, ok.

I can’t offer much, since I lack time and skill (I know almost nothing about compilerish stuff and I just started learning kotlin), but I’d like to give anyway my 2 cent availability for this long-term project

Where could we start from?

Please start with establishing a thorough understanding of how existing primitive types work in Java and in Kotlin, on the level of the type system and bytecode. After that, please read http://docs.scala-lang.org/sips/completed/value-classes.html and watch Brian Goetz’ talks on Project Valhalla from the JVM Language Summit.

1 Like

In the meanwhile, I wrote this small util for boxed unsigned types and unsigned operations on primitives