-
Notifications
You must be signed in to change notification settings - Fork 356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unsigned integer types #135
Comments
I have many case scenarios where I'd need to use And since this proposal doesn't contemplate extending the |
@elect86 Could you please elaborate on your use-cases? Maybe it'll be more convenient if we introduce new common supertype for unsigned types, for example, Also, just to note that if we'll decide to add |
Sure, for example in imgui I have a generic I'd like to have the possibility to include also unsigned int as well.
Those conversion methods make, I'd say, actually sense. Unsigned Int is actually a Number and it should have its corresponding |
What would the analog of |
@voddan The analog is just |
@ilya-g Oh, of course! Silly of me to forget how |
Playing with them I saw that If you leave it accessible you let the people (me) the possibility to access it and write comfortable operators overloads for arithmetic operation involving signed and unsigned operations i.e: |
@elect86 You can use |
Yeah, you are right. Anyway, which are the reasons behind making it private? |
@elect86 consistency with other numerical types? Byte is also just an int under the hood on JVM |
I've come across two potentially related issues when using the unsigned values in the latest 1.3-M1 release along with Delegation (it may affect other scenarios, such as other inline classes, but I've not seen any yet) I've raised https://youtrack.jetbrains.net/issue/KT-25784 and https://youtrack.jetbrains.net/issue/KT-25785 with examples |
@rossdanderson Thank you! We'll try to address these issues soon |
There are no |
@mquigley The string-to-number and number-to-string conversions are planned for the next milestones. |
For int arithmetic with floats and double we have the logic that Any reason why this widening operation should be different for unsigned ints? I.e. when It just seems logical, because To me it seems more consistent. |
I saw extensions like |
@cretz
We should also provide some copying functions like |
@stangls Kotlin is a statically typed language. The return type of an arithmetic operation depends on the types of operands but not on their values. So to introduce an arithmetic operation between signed and unsigned operands, you need to specify what is the return type for a given combination of operand types. As you can see, both ways to declare the operation may produce an overflow and that overflow may be not that the author intended to have, thus it can result in subtle bugs. We require author to declare explicitly which type of operation is used, either signed or unsigned one, by converting operands to compatible types. |
@ilya-g : You are right, Kotlin is a statically typed language. Any yes, that is exactly why you read my example right, because you read values that have an unambiguous type. |
Are there any plans to support (or wrap) different descendants of |
@0legg We have no such plans for 1.3, though nothing seems to prevent providing such support in a separate library. |
Is it planned to make some kind of safe interop with java? For example, if you export Maybe it is possible to do it with some external tool. Like android studio does with |
@zayass The unsigned classes are implemented as Kotlin inline classes. public inline class UShort internal constructor(private val data: Short) So, a I recommend reading all about Kotlin inline classes to get a better picture of what's going on under the hood. But you can always write a few test classes yourself. |
That's not true. Commenter even referenced boxed forms. I too recommend reading all about Kotlin inline classes. Granted I'd say the Kotlin forums are better for conversations like these. |
@mquigley @cretz I understand how it works and how it look in bytecode, but have issues with current implementation. Where is a proper place for discuss such issues? For example: "Expose or "Mark inlined values with annotation for external tooling" |
@zayass Declarations inside unsigned classes and declarations that have unsigned types in their signatures on top-level will be mangled. In other words, it will not be possible to use functionality of unsigned types from Java. Please check out these sections from inline classes KEEP for more details: overloads, non-public constructors, mangling |
@zarechenskiy I understand overloading issue when type is inlined. But mangling leads to another issue in kotlin java interop. For example we have mixed project with java/kotlin/c and want to use unsigned types for better interop with c. And it works cool with kotlin<->c, but not with java<->kotlin<->c. And what is more strange, interface methods like Why not to export methods with boxed arguments to java without mangling? Like this
|
@zayass Because this way we'll have to duplicate method count for inline classes and declarations that use inline class types in their signatures. Also, we'd love to make inline classes future-proof with regard to project Valhalla: we'd love to make these classes value classes and maybe at some point, we'll generate similar declarations if we'll be sure about gradual migration. |
Sounds reasonable thanks |
I'm not sure if I'm at the correct location for reporting this finding, but opening a issue in YouTrack sounded a bit to premature to me. I gave the feature a small try today and noticed that the unsigned type conversion functions are still missing for float and double. The types provide conversion functions to signed integral types (e.g. toByte() ) but the extensions provided by e.g. UByte are covering them: I'm using kotlinc-jvm 1.3-M2. Is it planned for 1.3 to add those conversions? |
@Danielku15 Please, use latest, 1.3 RC-4 ( |
Try with IntelliJ IDEA EAP 2018.3 and after you update the Kotlin plug-in,
also update your project to use the latest version
…On Sun, Oct 28, 2018, 11:24 AM Daniel ***@***.***> wrote:
@LouisCAD <https://github.com/LouisCAD> Thanks for the fast response. I
pulled the latest update available on the "Early Access Preview 1.3"
channel via IntelliJ just today. Even though it was announced here
<https://discuss.kotlinlang.org/t/kotlin-1-3-rc4-is-available/9900> that
1.3-RC4 is available via the EAP channel I cannot install it from there. Am
I missing something?
My Environment:
- IntelliJ IDEA 2018.2 (Community Edition)
- Windows 10 Pro N 1803 64bit
[image: image]
<https://user-images.githubusercontent.com/674916/47614674-c8e86d00-daa3-11e8-959f-8921b3011474.png>
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#135 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AGpvBZ282Rcw7io5GikkuCJDPVrA-Oh5ks5upYXGgaJpZM4VPbgH>
.
|
@Danielku15 @LouisCAD Let's keep the discussion not far from unsigned types. Regarding |
Is the difference between Java's println((-1).toULong().toString(16))
println(Integer.toUnsignedLong(-1).toString(16))
println((Integer.MIN_VALUE).toULong().toString(16))
println(Integer.toUnsignedLong(Integer.MIN_VALUE).toString(16)) Output:
It looks like Kotlin assumes you want to convert your signed int to an unsigned long, whereas Java assumes that the integer you provide is an unsigned int, and you're converting it to an unsigned long. This was surprising to me, because I typically want to take some Java APIs output and convert it to |
@kenzierocks Yes, the difference is intentional. The initial implementation of unsigned types had The reason why This approach is not required when the language clearly distinguishes unsigned types from signed ones. In Kotlin you do not need to assume that a singed So to summarize:
|
Nicely put! Please make sure to include this summary into the final documentation for unsigned arithmetic ;) |
Hi All. Thanks for all the work on this, and I would just like to highlight a use-case. I'm currently busy with EMV development, and having an unsigned byte primitive (and associated array types) is essential to directly handling byte and bit-level manipulations. I would be very sad to see support for unsigned types dropped in future, and I imagine so would everyone else who is looking to do bit-level work in Kotlin. Again, thanks for this important contribution to an excellent language. |
I'd like to leave a few comments about quality of life issues using unsigned values in Kotlin.
val a1: UShort = 1u;
val a2: UShort = 2u;
val a3 = a1 + a2 Reasonably one would expect the type of
int var4 = UInt.constructor-impl(a1 & '\uffff');
int var5 = UInt.constructor-impl(a2 & '\uffff');
int a3 = UInt.constructor-impl(var4 + var5); I'm just passing this feedback along to see if it can help in anyway. The current state of affairs is much better than not having unsigned types at all! (1) private fun Any.toInt(): Int {
if (this is UByte) return this.toInt() // I can't group all the unsigned types
if (this is UShort) return this.toInt()
if (this is UInt) return this.toInt()
if (this is ULong) return this.toInt()
if (this is Number) return this.toInt() // I can group all signed types as a Number
throw IllegalArgumentException("Not number $this")
} |
@matt-quigley-ck I'd like to point you to one of our library, kotlin-unsigned in case it may suit better your needings:
Kotlin stdlib unsigned is lighter because they are |
Will there be any compile time checks to prevent negative unsigned numbers? fun bar() {
foo(4u - 5u) // no error, but could be optimized to foo(-1u) and should fail at compile time
foo(-1u) // compiler error, unaryMinus not found
}
fun foo(i: UInt) { } |
This should not fail, it should wrap around. Wrap arounds are completely legal, valid, and common in unsigned work. Note that some languages do handle integer overflow handling in a first class manner. (Rust and Swift come to mind) But it's never been a Java thing. If this is wanted, I'd say helper methods are the way to go. |
There's no such thing as negative unsigned numbers, because unsigned numbers do not have a sign. A situation like An overflow could be detected for unsigned numbers too. I've opened KT-46470 to track this feature. |
An observation was reported about the fact that comparison operators mixing signed and unsigned integer types have clear meaning and potentially could be provided: https://youtrack.jetbrains.com/issue/KT-59634/Provide-comparison-operators-that-mix-signed-and-unsigned-operands |
Java standard library has Kotlin's unsigned types could benefit from a similar set of functions. For example:
Bonus functions:
|
This issue is for discussion of the proposal to introduce unsigned integer types in Kotlin.
The text was updated successfully, but these errors were encountered: