Skip to content

Instantly share code, notes, and snippets.

@ecoopnet
Last active January 6, 2023 10:50
Show Gist options
  • Save ecoopnet/288d7284bc1c6dc3f6070201afd5e8ec to your computer and use it in GitHub Desktop.
Save ecoopnet/288d7284bc1c6dc3f6070201afd5e8ec to your computer and use it in GitHub Desktop.
Dumps collections as String
// 任意のコレクション型に文字列を返す dump メソッドを追加する。
private fun Any.dump(): String {
if (this is Map<*, *>) {
return dump()
}
if (this is List<*>) {
return dump()
}
if (this is Collection<*>) {
return dump()
}
return toString()
}
private fun <K,V> Map<K,V>.dump(): String {
return map { (k, v) ->
"$k:[${v?.dump()}]"
}.joinToString("\n")
}
private fun <V> Collection<V>.dump(): String {
return joinToString(",")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment