Skip to content

Commit

Permalink
Allow registration of custom Show typeclasses #2021
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed Jan 29, 2021
1 parent 60374c7 commit 4cf8361
Showing 1 changed file with 5 additions and 2 deletions.
Expand Up @@ -74,8 +74,11 @@ expect fun <A : Any> platformShow(a: A): Show<A>?
@Suppress("UNCHECKED_CAST")
fun <T : Any> commonShowFor(t: T): Show<T> {
// lookup a show from the registered typeclasses
val kclass = Shows.all().keys.firstOrNull { it.isInstance(t) }
if (kclass != null) Shows.all()[kclass] as Show<T>
val kclass: KClass<*>? = Shows.all().keys.firstOrNull { it.isInstance(t) }
if (kclass != null) {
val show: Show<*>? = Shows.all()[kclass]
return show as Show<T>
}
// this won't work in JS or native, so they'll get the boring old toString version
if (io.kotest.mpp.reflection.isDataClass(t::class)) return dataClassShow<T>()
return DefaultShow
Expand Down

0 comments on commit 4cf8361

Please sign in to comment.