How can I access the "new" (since stdlib 1.7) `Ite...
# getting-started
l
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
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
This is what I have. Use maxByOrNull()
j
But Lukasz's goal is to use the
maxBy
from Kotlin 1.7+ that returns a non nullable result
a
If you see his screenshot maxBy() is returning T?
j
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
So it’s nullable 🙂 maxBy() and maxByOrNull() are both nullable
s
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
According to last line, looks like it takes this from gradle deps. I’m already using 2.0 Beta1
l
Doesn't this annotation
@Suppress("CONFLICTING_OVERLOADS")
suggest that both the old and new functions are ambiguous? Doesn't that confuse the IDE?
s
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
The weird thing is, the old method should not even be there in the stdlib since 1.7 I think.
s
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
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
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
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
Yes, indeed - it actually throws a
NoSuchElementException
.
k