How do I compare a Short with an Int in Kotlin? - kotlin

I have a Short variable that I need to check the value of. But the compiler complains that Operator '==' cannot be applied to 'Short' and 'Int' when I do a simple equals check:
val myShort: Short = 4
if (myShort == 4) // <-- ERROR
println("all is well")
So what's the simplest, "cleanest" way to do this equals check?
Here are some things I tried.
The first one casts the 4 integer to a short (looks weird, invoking a function on a primitive number)
val myShort: Short = 4
if (myShort == 4.toShort())
println("all is well")
The next one casts the short to an int (shouldn't be necessary, now I have two ints when I shouldn't really need any)
val myShort: Short = 4
if (myShort.toInt() == 4)
println("all is well")

Basically, the 'cleanest' way to compare it with a small constant is myShort == 4.toShort().
But if you want to compare a Short with a wider-type variable, convert myShort instead to avoid the overflow: myShort.toInt() == someInt.
looks weird, invoking a function on a primitive number
But it does not actually call the functions, they are intrinsified and compiled to bytecode that operates the numbers in a way that is natural for JVM, for example, the bytecode for myShort == 4.toShort() is:
ILOAD 2 // loads myShort
ICONST_4 // pushes int constant 4
I2S // converts the int to short 4
IF_ICMPNE L3 // compares the two shorts
See also: another Q&A concerning numeric conversions.

You could also create an infix function instead of ==. I called it eq
infix fun Short.eq(i: Int): Boolean = this == i.toShort()
And you can use it like this
val myShort: Short = 4
if (myShort eq 4)
println("all is well")

Related

Assignment after not null check

I expected that the type of a variable is promoted to a non-null type after a not-null check (like in the Dart language).
val someMap = mapOf("a" to 0L)
val a = someMap['a'] // a is of type Long?
if (a != null) {
val b = a // b is of type Long? and not of type Long. Why?
}
Can someone explain why this is not the case? Just a matter of taste of the language designers?
Since there is smart-casting, it doesn't matter. It will allow you to use members of a or b inside the if statement without null-safe calls (?.) or null assertions (!!). You can also safely declare b to be a Long without the compiler complaining:
if (a != null) {
val b: Long = a
}
It is I think a design choice for how implicit types should be inferred that b's type must be explicitly declared if you want it to be considered non-nullable. This is only relevant if passing it to a function with generics, since there is smart-casting.
What you can do instead of explicit null check is using let{} as follows:
val someMap = mapOf('a' to 0L)
val a = someMap['a'] // a is of type Long?
a?.let {
val b = it // b is of type Long
}
It is called smart casting, basically Kotlin is smart enough to determine that variable can no longer be null after check. More detail and can be found here if you are interested
As to why, only the creators of kotlin can know. But what you can do is this if you want a Long instead of Long? there is this
val b = a!!

Why is my Kotlin comparable not makign correct equality comparisons?

I am currently trying to make a comparable object and working on the compareTo() function, for which I wrote the following code
class InfoAcad(e: String, m: String, c: Int): Comparable<InfoAcad> {
override operator fun compareTo(other: InfoAcad): Int {
if (this.e < other.e) return -1
if (this.e > other.e) return 1
if (this.e == other.e && this.m < other.m) return -1
if (this.e == other.e && this.m > other.m) return 1
return 0
}
}
The idea is that e is an ID number inputted as a string, which always follows the format XX-XXX where every X character is an integer between 0 and 9, and m is a course code following the format LL-XXX where each L character is a capital letter between A and Z and the X characters are integers between 0 and 9 like in the ID numbers. The objects are first compared by their ID number, and if the ID numbers are equal they are then compared by the course code, if both values are the same then the objects are equal, the c parameter is not taken into account in the comparison. I found out yesterday that I could compare strings directly in Kotlin in < and > relations, so I decided to try using that to make the task of comparing the InfoAcad objects a bit easier on myself, however when I make a main function to test the comparisons, the equality always returns a false value independently of what is in the string values of the InfoAcad objects. Here's said main function:
fun main() {
var A = InfoAcad("18-10125", "CI-2526", 3)
var B = InfoAcad("18-10125", "CI-2526", 5)
println("A = B: " + (A == B).toString()) //true
println("A < B: " + (A < B).toString()) //false
ptintln("A > B: " + (A > B).toString()) //false
}
When I change characters in the ID and course code values the inequality relations work just as intended, so what could be causing the equality relation to always return false? I appreciate and thank any responses in advance.
Note: I have also tried giving A and B the same c value, the equality part still returned false.
Override the equals function as well, or use a data class.
compareTo is only used for the < and > operators. The == operator is implemented by the separate equals function.
You can find the available operators, and the functions you need to override for each, in the Operator overloading section of the Kotlin docs.
If you don't override the equals function, the default behaviour is for it to use object identity. That means that two different objects, even if they contain the same fields, will never be considered equal.
There is however a nice shortcut for what you want to do! Kotlin will automatically generate an equals function for you if you make your class a data class. It's a good fit for classes like yours, whose main purpose is to hold data.
Because == and != translates to a call to equals(other: Any?): Boolean method, from kotlinlang
Expression Translated to
a == b a?.equals(b) ?: (b === null)
a != b !(a?.equals(b) ?: (b === null))
These operators only work with the function equals(other: Any?): Boolean, which can be overridden to provide custom equality check
implementation. Any other function with the same name (like
equals(other: Foo)) will not be called.

Input out of range for Int datatype, not passing a testcase

I am trying to solve the following question on LeetCode; Write a function that takes an unsigned integer and returns the number of '1' bits it has. Constraints: The input must be a binary string of length 32.
I have written the following code for that which works fine for inputs 00000000000000000000000000001011 and 00000000000000000000000010000000 (provided internally by the website) but give output 0 for input 11111111111111111111111111111101 and in my local compiler for the last input it says "out of range"
class Solution {
// you need treat n as an unsigned value
fun hammingWeight(n:Int):Int {
var num = n
var setCountBit = 0
while (num > 0) {
setCountBit++
num= num and num-1
}
return setCountBit
}
}
To correctly convert binary string to Int and avoid "out of range error", you need to do the following (I believe LeetCode does the same under the hood):
fun binaryStringToInt(s: String): Int = s.toUInt(radix = 2).toInt()
"11111111111111111111111111111101" is equivalent to 4294967293. This is greater than Int.MAX_VALUE, so it will be represented as negative number after .toInt() convertion (-3 in this case).
Actually, this problem could be solved with one-liner in Kotlin 1.4:
fun hammingWeight(n: Int): Int = n.countOneBits()
But LeetCode uses Kotlin 1.3.10, so you need to adjust your solution to handle negative Ints as well.
Please change the type of your input variable from Int to a type like Double .At the moment The given value is bigger than the maximum value that a type Int number can store.

How to print ClosedRange in Kotlin

I'm learning Kotlin , and I'm trying to undetstand Ranges
I created a range of String as follows
val alpha = "A".."Z"
I want to print this for that I wrote
for (item in alpha) println(item)
But it gives the error
Error:(13, 18) Kotlin: For-loop range must have an 'iterator()' method
Can anyone help, how to print this range?
Well you can't do it with Strings by default, since there's no iterator() for ClosedRange<String>, but Chars will work directly:
val r = 'A'..'Z'
r.forEach(::println)
It will be of type CharRange and provide the needed iterator().
To make your very special example work with Strings, you could define your own extension function and delegate to a Iterator<Char>:
operator fun ClosedRange<String>.iterator(): Iterator<String> {
val charIt = (start.toCharArray().first()..endInclusive.toCharArray().first()).iterator()
return object : Iterator<String> {
override fun hasNext() = charIt.hasNext()
override fun next(): String = charIt.nextChar().toString()
}
}
Now it works as you wished. But be aware that this does not make sense for most use cases with ranges of String.
val alpha = "A".."Z"
This is a plain range, which means it's an abstract representation of a contiguous subset within a total order. The only operation such an entity supports is answering the question "is this element within this range?", and it will be based purely on the contract of Comparable<T>.
In your case, consider a string like "THREAD". Does it belong to your range? It sorts higher than "A" but lower than "Z", so it does belong to it. But you probably didn't intend to iterate over it, or the infinity of all other strings belonging to your range.
What you considered as a given is actually a special case: iterable ranges. They are defined only on the three types representing integral types: IntRange, LongRange and CharRange. These are the types where the subset belonging to the range can actually be enumerated and iterated over.
I Loved The answers of #s1m0nw1 and #Marko Topolnik.
You can't really iterate over a ClosedRange<String>.
The literal may mislead us to think it's a ClosedRange<Char>
var range = "A".."C" // misleading literal
What you CAN do:
Just a small addition: is to map the characters to Strings
val r = ('A'..'Z').map { it.toString() }//.forEach(::println)
for (letter in r){
}
How to print this range?
I think the only way to print this range is
println(alpha)
And you'll get
A..Z
This is how to "print" this range.
You're trying to travel through a non-iterable range, this is invalid.
Like, you cannot for (i in File("a.txt")..File("Main.java")) println(i).
Can You Try This May Help You, I think you actually want 'A'..'Z' not "A".."Z"
var A = 'A'..'Z'
for(value in A){
println("$value")
}

Equal operator cannot be applied between primitive and boxed variable

In Kotlin it is trivial to compare some Long value to 0 like this.
if (firstUnread == 0) {
return
}
But usually this error occurs. What is the best way to compare these values?
Operator '==' cannot be applied to 'Long' and 'Int'
0 is an Int in Kotlin.
try to replace it with 0L instead and it will work.
In Kotlin, there are no implicit widening conversions for numbers: https://kotlinlang.org/docs/reference/basic-types.html
In your code you are attempting to compare a Long (firstUnread) to an Int (0). Instead compare to 0L so you are comparing a Long to a Long.
You can easily add .toLong() method after any int number.
Just change your code to this:
if (firstUnread == 0.toLong()) {
return
}