https://kotlinlang.org logo
#getting-started
Title
# getting-started
l

Lukasz Kalnik

12/10/2023, 4:29 PM
How can I access the "new" (since stdlib 1.7)
Iterable.maxBy()
method? When I just write it out, it references the nullable deprecated variant:
j

Joffrey

12/10/2023, 4:36 PM
I'm not sure how you get this, mine shows the correct function:
I can't even find the deprecated
maxBy
in the 1.9.21 stdlib. Do you get any deprecation warning in the IDE? Or when running gradle via CLI?
a

Andrei Kovalevsky

12/10/2023, 4:37 PM
This is what I have. Use maxByOrNull()
j

Joffrey

12/10/2023, 4:39 PM
But Lukasz's goal is to use the
maxBy
from Kotlin 1.7+ that returns a non nullable result
a

Andrei Kovalevsky

12/10/2023, 4:40 PM
If you see his screenshot maxBy() is returning T?
j

Joffrey

12/10/2023, 4:40 PM
Yes, that's exactly the problem. He's using Kotlin 1.9.21, which shouldn't have the nullable variant, and should have the non-nullable variant
a

Andrei Kovalevsky

12/10/2023, 4:40 PM
So it’s nullable 🙂 maxBy() and maxByOrNull() are both nullable
s

Sam

12/10/2023, 5:29 PM
Huh. When I try it, IntelliJ gets a bit confused. These two screenshots are the same line of code.
Tried updating the IDE to see if it would fix it. Now it shows this 😂
😮 1
a

Andrei Kovalevsky

12/10/2023, 7:18 PM
According to last line, looks like it takes this from gradle deps. I’m already using 2.0 Beta1
l

Lukasz Kalnik

12/11/2023, 8:49 AM
Doesn't this annotation
@Suppress("CONFLICTING_OVERLOADS")
suggest that both the old and new functions are ambiguous? Doesn't that confuse the IDE?
s

Sam

12/11/2023, 8:51 AM
I think that must be what's happening, yeah. Certainly my IDE was very confused; in my last screenshot you can see that it's actually combined the docs from both methods 😬
l

Lukasz Kalnik

12/11/2023, 8:52 AM
The weird thing is, the old method should not even be there in the stdlib since 1.7 I think.
s

Sam

12/11/2023, 8:57 AM
No, it's still there. • Old: https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/jvm/src/generated/_CollectionsJvm.kt#L88 • New: https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/common/src/generated/_Collections.kt#L1937 I'm guessing the old one is hidden so it can't be referenced in new code, but can still be used at runtime by code that was built pre-1.7.
Maybe IntelliJ doesn't understand the
@DeprecatedSinceKotlin
annotation properly?
l

Lukasz Kalnik

12/11/2023, 9:01 AM
Deprecated only means a deprecation warning should be shown (which by the way isn't the case for me - the function is not marked yellow and no warning is displayed).
s

Sam

12/11/2023, 9:04 AM
Kotlin actually has three different levels of deprecation. The default is a warning, like you say, but you can also have it be an error or even have it hide the declaration entirely as if it doesn't exist. In this case, the
@DeprecatedSinceKotlin
annotation is saying that it should be a warning in 1.4, an error in 1.5 and an unresolved reference in 1.6—even though the function still exists at runtime. Docs: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-deprecated-since-kotlin/
🙏 1
As you say, it's weird that it's not doing any of those three things, though. Definitely seems like an IDE bug of some kind.
s

Sam

12/11/2023, 9:34 AM
Out of curiosity, does it resolve to the right one when you actually run it? In my case, the IDE shows the wrong one but it does the right thing at runtime.
l

Lukasz Kalnik

12/11/2023, 9:41 AM
Yes, indeed - it actually throws a
NoSuchElementException
.
k

Klitos Kyriacou

12/11/2023, 2:15 PM