K J
03/19/2023, 9:16 PM// Dummy extension needed for compilation. Without this, IntelliJ will not compile our program
val Any.test: String
get() = "Will never Run"
fun main() {
println(4.test) // => "Test String" (our injected property return value)
}
How can I get around this / get IntelliJ to recognize the synthetic property?vladimirsitnikov
03/20/2023, 5:15 AMK J
03/20/2023, 6:48 AMK J
03/20/2023, 7:01 AMvladimirsitnikov
03/20/2023, 7:02 AMval Any.test: String
once and add it as a dependency?
If so, that might be the best option as you won’t need to generate and compile it every timevladimirsitnikov
03/20/2023, 7:03 AMvladimirsitnikov
03/20/2023, 7:03 AMvladimirsitnikov
03/20/2023, 7:04 AMK J
03/20/2023, 7:06 AMvladimirsitnikov
03/20/2023, 7:08 AMK J
03/20/2023, 7:12 AMvladimirsitnikov
03/20/2023, 7:14 AMK J
03/20/2023, 10:17 AMK J
03/20/2023, 10:20 AMval a = Query(table_a) // dynamically has all the columns of table_a as props
val b = Query(table_b) // same for table_b
val newQuery = a.naturalJoin(b) // newQuery should now dynamically have all the properties of A and B
Youssef Shoaib [MOD]
03/29/2023, 4:59 AMTableA
for every table.
Have it also generate extension vals Query<TableA>.propA
for every column of TableA
Now, by suppressing a compiler error (I know I know, it's unsupported, but it sure seems more reliable than the compiler plugin API right now) we can get an intersection type and thus have our newQuery
be considered both a query on TableA
and a query on TableB
, thus the columns of both tables are valid on it. (Playground):
class Query<Table>(val tables: List<Table>) {
constructor(table: Table): this(listOf(table))
}
// Misusing type bounds to get an intersection type
@Suppress("BOUNDS_NOT_ALLOWED_IF_BOUNDED_BY_TYPE_PARAMETER")
fun <Q1: Query<*>, Q2: Query<*>, QR> Q1.naturalJoin(other: Q2): QR
where QR: Q1, QR : Q2
{
val result = Query<Any>(tables + other.tables)
return result as QR
}
fun main() {
val a = Query(TableA) // has all the columns of TableB as props
val b = Query(TableB) // same for TableB
val newQuery = a.naturalJoin(b) // newQuery should now have all the properties of A and B
println(newQuery.strA + newQuery.strB)
println(newQuery.intA + newQuery.intB)
}
// Generated by plugin
object TableA
object TableB
val Query<TableA>.strA: String get() = "foo"
val Query<TableA>.intA: Int get() = 7
val Query<TableB>.strB: String get() = "bar"
val Query<TableB>.intB: Int get() = 42