y
09/23/2024, 1:21 PMclass Foo
and I'd like to override Foo + String
and String + Foo
, such that both return a Foo
.
the first part is easy enough, just implement operator fun plus(s: String): Foo
inside the definition of class Foo
.
for the second part, attempting to add an extension function operator fun String.plus(foo: Foo): Foo
makes kotlin complain that I'm shadowing operator fun plus(other: Any?): String
is there a way around this (and should I even go that way)?Vampire
09/23/2024, 1:36 PMRobert Williams
09/23/2024, 1:50 PMlog("error in foo: " + myFoo);
with the expectation this would call myFoo.toString and add to the log message.
If what you propose were possible, it'd instead construct a new Foo and then call toString on that, which would be a completely unexpected and confusing log message.
There's probably even more serious things it would also break but this alone would be reason to avoid it.Daniel Pitts
09/23/2024, 2:05 PMy
09/23/2024, 5:14 PM