KotlinPoet: Do you know a possibility to prevent `...
# squarelibraries
h
KotlinPoet: Do you know a possibility to prevent
CodeBlock.of("${CodeBlock.of("foo")}.bar")
? This calls
CodeBlock.toString()
and results into wrong imports. You must use
CodeBlock.of("%L.bar", CodeBlock.of("foo"))
to keep the arguments of the inner CodeBlock.
p
Did you already answer your own question?
h
No 😄 I want to prevent the wrong usage to not shoot yourself in the foot. Otherwise you can generate wrong code.
p
Ah your mean really to prevent it. Tests i assume 😁
☝️ 1
g
Just an idea, on this project I used a class to be able to concatenate strings with arguments.
Copy code
"val t: %T".toFormatString(customType) +
  " = %M()".toFormatString(methodRef) + ...
The transformation in code blocks is only done in the end, so I presume it saves me for this burden. And to ensure I'm not using toString(), I throw an error from the toString method 🙂
h
Yeah, I thought the same. What about specifying
toString(): Nothing
too?
g
Good idea!