alexhelder
03/25/2025, 6:44 PMSaverScope.Save
I don’t grok. Here is the easy part:
interface Saver<Original, Saveable : Any> {
fun SaverScope.save(value: Original): Saveable?
}
fun interface SaverScope {
fun canBeSaved(value: Any): Boolean
}
Saver
function below what confuses me, specifically save.invoke(this,value)
fun <Original, Saveable : Any> Saver(
save: SaverScope.(value: Original) -> Saveable?,
): Saver<Original, Saveable> {
return object : Saver<Original, Saveable> {
override fun SaverScope.save(value: Original) = save.invoke(this, value)
}
}
The object instantiation is implementing save
with save.invoke(this, value)
but afaict this
is not SaverScope
, it is an object of type Saver
. Can somebody clarify what is happening here?Joffrey
03/25/2025, 6:46 PMthis
is not SaverScope
It is. The this
in save.invoke(this, value)
refers to the receiver of the save
function implemented in the anonymous object. It doesn't refer to the anonymous object itself (you would need some qualified this@something
to access the outer this
in this case).alexhelder
03/25/2025, 6:51 PM