alexhelder
04/16/2025, 8:46 PMsealed interface Thing {
value class Aaa:Thing
value class Bbb:Thing
value class Ccc:Thing
}
alexhelder
04/16/2025, 8:53 PMPrefSaver
like
interface PrefSaver<T> {
fun T.save(prefs)
fun load(prefs):T
}
And then do something like
value AaaSaver = object:PrefSaver<Aaa> {
....
}
How can I associate the subclass with the actual saver? In the article I see things like
with (AaaSaver) {
do something
}
But I can’t figure out how , if you only know the base class Thing
, to associate the Type Class Instance for it:
fun process(thing:Thing) {
val saverForThing = ???
with (saverForThing) { ... }
}
All I could think of was pattern matching using when
, but the article seems to imply this could be eliminated. I was trying to move away from
when (thing) {
is Aaa -> save(aaa)
is Bbb -> save(bbb)
is Ccc -> save(ccc)
}
CLOVIS
04/17/2025, 7:17 AMval Thing.saver get() = object : PrefSaver<Thing> {
// …
}
this way you can:
fun process(thing: Thing) {
with (thing.saver) { ... }
}
Wout Werkman
04/20/2025, 3:15 PMPrefSaver<Thing>
. I don't exactly see the syntax you are hoping to achieve.