vach
vach

Reputation: 11377

Kotlin to override protobuf types toString

Suppose we have some data classes which are generated with protoc.

Google's toString() sucks for multiple reasons, it's not very human-readable, and even puts line breaks in representation which screws up logs and many tools that don't expect multiline log.

So the problem is: how do we redefine custom toString() for a class like that?

I've tried to override (or maybe hide?) original toString() with an extension function like this

fun Messages.DataClass.toString(): String {
    return "some custom logic"
}

but it won't pick up the extended function.

Upvotes: 2

Views: 1014

Answers (1)

yole
yole

Reputation: 97268

It's not possible to change the implementation of an existing method in a class using Kotlin. An instance method defined in a class always takes priority over an extension function with the same signature.

Upvotes: 5

Related Questions