class Where(
val column: String,
var value: Any?,
val condition: String? = null,
) : IWhere {
override fun clone(): Where {
val clone = Where(column, value, condition)
if(::callback.isInitialized)
clone.setCallback(callback)
return clone
}
private lateinit var callback: (Player) -> Any?
override fun runCallback(player: Player){
if(!::callback.isInitialized) return println("its not initialized")
println(callback)
value = callback(player)
println("New Value $value")
}
fun setCallback(callback: (Player) -> Any?) : Where {
this.callback = callback
return this
}
}
class SelectQuery(
id: Int, val table: String, val where: IWhere? = null
) : Query(Query.TYPE_SELECT, id){
override fun clone() : SelectQuery {
return SelectQuery(id, table, where?.clone())
}
}
settings.queries.add(
DataManager.database.prepareSelect(
TABLE,
Where.PLAYER("").setCallback { player -> player.name } ,
{ result -> println("Successful.") }
)
) # This adds the query to the DependencyInfo
val queries = dependencyInfo.queries.map { it.clone() }.toMutableList()
queries.forEach { query ->
if(query is SelectQuery){
query.where?.runCallback(player)
}else if(query is InsertQuery){
query.runCallbacks(player)
}
}
on runCallback
it prints NEW VALUE kotlin.Unit even though the return type is Any?
i tried value = callback(player) as Any? but it says i cannot convert kotlin.unit to any?
I really don’t understand whats the problem here, i hope you can help.
It’s hard to understand exactly what’s happening since this isn’t your complete code. But from what I’m seeing, there’s two things that it could be.
First, the value variable could be being changed by something else, since it’s a public var.
Second, Player.name is of type Unit.
If neither of those are the case, then I’m really not sure. I’d need to see more of your code. That said, your code seems to have a lot of mutability, which makes things harder. Kotlin favours immutability for a reason.
I am not changing the value from anywhere else. I just use runCallback to change it. The unit works and executes the code but it returns Kotlin.Unit even though i am certainly returning a string. And Player.name is certainly a string. And i even tried println(callback(player)) which must return a string yet it returned kotlin.Unit
Try putting a println in your actual callback lambda.
Where.PLAYER("").setCallback { player ->
println("Running call back function, player: $player, player name: ${player.name}")
player.name
}
1 Like
I already tried that and it prints a string. It should work with no problem but i don’t understand whats the problem
. I am not changing the callback to another callback i am just copying it so the class doesn’t have a reference to the original one. Something is broken, i don’t know.
What does DataManager.database.prepareSelect( take as arguments, and what does it do? I’m wondering if the second lambda you’re passing is somehow the one being set on the Where, since that second lambda does return a Unit.
1 Like
Nevermind bro i fixed it. It was because the database.jar file i added somehow was broken or compiled wrong i don’t know really but it was detecting the return type as kotlin.Unit:
private static final kotlin.Unit onLoad$lambda$1(cn.nukkit.Player);
Code:
0: aload_0
7: aload_0
8: invokevirtual cn/nukkit/Player.getName()Ljava/lang/String;
11: pop
12: getstatic kotlin/Unit.INSTANCE
15: areturn
i fixed it by replacing the database.jar with the not broken one
i just checked if it was because i added return@setCallback and it wasn’t because of that and it was because of what i exactly said, thank you for your time
2 Likes