Does Kotlin provide a way to easily diff two objec...
# getting-started
r
Does Kotlin provide a way to easily diff two objects, recursively?
Copy code
foo.bar.baz.x // 5
val copy = foo
foo.bar.baz.x = 7

diff(copy, foo) // { field: .bar.baz.x, before: 5, after: 7 }
a
AFAIK not. It isn’t a very trivial problem. For a previous project I rolled my own. But I guess that with some compiler plugin magic a lot is possible. With reflection you can also get pretty far.
👆 1
j
In which form would you expect this to happen? What type would
diff
return? If you want to diff for debug purposes or in a textual form in general, you could consider serializing to JSON and diffing the JSON as text (there are libraries for this AFAIK)
2
For instance this one might help with the text diff: https://github.com/GitLiveApp/kotlin-diff-utils
j
That "copy" only copied the reference so there won't actually be a diff.
think smart 1
blob think smart 1
🧠 1