LeoColman
10/27/2024, 3:32 PMinternal fun Iterable<*>.containerName(): String {
return when (this) {
is List -> "List"
is Set -> "Set"
is Map<*, *> -> "Map"
is ClosedRange<*>, is OpenEndRange<*> -> "Range"
is Collection -> "Collection"
else -> "Iterable"
}
}
Why does this code compiles when Map
and Ranges
are not Iterable?
AI caught this issue for me and I expected the compiler to 😂Youssef Shoaib [MOD]
10/27/2024, 3:34 PMIterable
and a Map
(probably, although you might run into signature overload issues)LeoColman
10/27/2024, 3:41 PMokarm
10/27/2024, 4:20 PM1..2
gives you an IntRange
, which is an IntProgression
, which is an Iterable<Int>
. It is also an Open/ClosedRange
The Progression provides iteration and the Range provides start/end valuesokarm
10/27/2024, 4:37 PMMap
case makes much less sense though. I don't know if there are any standard Maps that implement Iterable.LeoColman
10/27/2024, 7:02 PMandries.fc
10/28/2024, 8:06 AMCollection<Map.Entry<K,V>>
this would have made the whole API more consistent.Thomas
11/01/2024, 2:17 AMfinal
or open
:
//final classifiers
class Class
enum class EnumClass
//open classifiers
interface Interface
open class OpenClass
abstract class AbstractClass
sealed class SealedClass
For example:
//(just for clarity in example)
typealias FinalClassifier = Class
typealias OpenClassifier = Interface
fun FinalClassifier.test() {
when (this) {
is Class -> {} //OK
is EnumClass -> {} //Error
is Interface -> {} //Error
is OpenClass -> {} //Error
is AbstractClass -> {} //Error
is SealedClass -> {} //Error
}
}
fun OpenClassifier.test() {
when (this) {
is Class -> {} //Error
is EnumClass -> {} //Error
is Interface -> {} //OK
is OpenClass -> {} //OK
is AbstractClass -> {} //OK
is SealedClass -> {} //OK
}
}
Thomas
11/01/2024, 2:21 AM