Amejonah 1200
04/15/2023, 9:36 PMis is a runtime operator, but what about whens?
The idea is the ability to make type arguments have values at compile time (it would be nice if every expression could have values as well), and so optimize code away or at least replace with a boolean value.
P.S.: I have no knowledge about K2 FIR, so I don't know about the possibility of it.
inline fun <reified T> printfIs(t: T) {
if (T is Int) println("Int: $t")
if (T is UInt) println("UInt: $t")
if (T is String) println("String: $t")
}
inline fun <reified T> printIfEq(t: T) {
if (T == Int) println("Int: $t")
// ...
}
inline fun <reified T> printWhenIs(t: T) {
when (T) {
is Int -> println("Int: $t")
// ...
}
}
inline fun <reified T> printWhenEq(t: T) {
when (T) {
Int -> println("Int: $t")
// ...
}
}dmitriy.novozhilov
04/17/2023, 7:46 AMAmejonah 1200
04/17/2023, 6:17 PMdmitriy.novozhilov
04/17/2023, 7:07 PMAmejonah 1200
04/17/2023, 7:09 PMdmitriy.novozhilov
04/18/2023, 6:45 AMAmejonah 1200
04/18/2023, 4:34 PM== and `is`: while == checks for equality where is could check inheritance as well (B is A <- B is a sub class of A)Amejonah 1200
04/18/2023, 4:40 PMdmitriy.novozhilov
04/18/2023, 4:40 PMAmejonah 1200
04/18/2023, 4:40 PMdmitriy.novozhilov
04/18/2023, 4:41 PMAmejonah 1200
04/18/2023, 4:43 PMAmejonah 1200
04/18/2023, 4:45 PMdmitriy.novozhilov
04/18/2023, 4:46 PMAmejonah 1200
04/18/2023, 4:46 PMdmitriy.novozhilov
04/18/2023, 4:47 PMAmejonah 1200
04/18/2023, 4:49 PMdmitriy.novozhilov
04/18/2023, 4:54 PMdmitriy.novozhilov
04/18/2023, 4:55 PMAmejonah 1200
04/18/2023, 4:56 PMdmitriy.novozhilov
04/18/2023, 4:56 PMdmitriy.novozhilov
04/18/2023, 4:57 PMAmejonah 1200
04/18/2023, 4:57 PMdmitriy.novozhilov
04/18/2023, 4:58 PMAmejonah 1200
04/18/2023, 4:58 PMAmejonah 1200
04/18/2023, 4:59 PMdmitriy.novozhilov
04/18/2023, 4:59 PMAmejonah 1200
04/18/2023, 5:00 PMAmejonah 1200
04/18/2023, 5:05 PMas could cause problems, as it is aware of inheritance. Do you see some problems in that?dmitriy.novozhilov
04/18/2023, 5:05 PMdmitriy.novozhilov
04/18/2023, 5:07 PMThey mentioned that as could cause problems, as it is aware of inheritance. Do you see some problems in that?Easily:
interface Base
class Derived : Base
inline fun <reified T> get(key: String): T? =
map[key]?.let {
when (T::class) {
Base::class -> it as T
else -> throw IllegalArgumentException("Not implemented for the given type.")
}
}
fun test() {
val x = get<Derived>("")
}Amejonah 1200
04/18/2023, 5:09 PMis but an equality, so ==Amejonah 1200
04/18/2023, 5:10 PMit as T, but this is a cast to something elsedmitriy.novozhilov
04/18/2023, 5:12 PMT::class will be inlined to Derived::class, so actually this whole function would be incorrect for this caseAmejonah 1200
04/18/2023, 5:12 PMdmitriy.novozhilov
04/18/2023, 5:13 PMAmejonah 1200
04/18/2023, 5:13 PMprivate inline fun <reified T> readBuf(
buf: ByteBuffer,
addr: UInt,
range: MemoryRange,
): T {
val offset = range.makeOffset(addr)
return when (T::class) {
UInt::class -> buf.getUInt(offset) as T
UShort::class -> buf.getUShort(offset) as T
UByte::class -> buf.uget(offset) as T
else -> throw AssertionError()
}
}
the idea came to be from this codeAmejonah 1200
04/18/2023, 5:16 PMas, but take a look at the utility. Tbf, this is a workaround of not having compile time traits in Kotlin so a when must be useddmitriy.novozhilov
04/18/2023, 5:20 PMAmejonah 1200
04/18/2023, 5:22 PMAmejonah 1200
04/18/2023, 5:25 PMTbf, this is a workaround of not having compile time traits in Kotlin so a when must be usedSo we missed Rust traits (and C++ templates) quite a lot, unsigned where not great (boxing and
as being a reference cast thing)Amejonah 1200
04/18/2023, 5:33 PMT is an expression so you don't need ::class anymore, would need a rewrite of the system. Is this right?dmitriy.novozhilov
04/19/2023, 3:42 PMAmejonah 1200
04/20/2023, 8:49 AMT is used as an expression you get an error as this isn't one. You need to specify ::class so get the class itself, not a "Type" in the sense of compile time construct.
So to enable T to be used as an expression, the type system and expressions must be adjusted to allow using Types as expressions.dmitriy.novozhilov
04/20/2023, 8:51 AMobject access, but it's a completely different story)Amejonah 1200
04/20/2023, 8:55 AMdmitriy.novozhilov
04/20/2023, 8:55 AMdmitriy.novozhilov
04/20/2023, 8:56 AMAmejonah 1200
04/20/2023, 8:57 AMAmejonah 1200
04/20/2023, 8:59 AMdmitriy.novozhilov
04/20/2023, 9:01 AMAmejonah 1200
04/20/2023, 9:05 AM