https://kotlinlang.org logo
#squarelibraries
Title
# squarelibraries
h

hfhbd

06/26/2022, 6:15 PM
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

Paul Woitaschek

06/26/2022, 6:37 PM
Did you already answer your own question?
h

hfhbd

06/26/2022, 7:01 PM
No 😄 I want to prevent the wrong usage to not shoot yourself in the foot. Otherwise you can generate wrong code.
p

Paul Woitaschek

06/26/2022, 7:15 PM
Ah your mean really to prevent it. Tests i assume 😁
☝️ 1
g

Grégory Lureau

06/27/2022, 7:47 AM
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

hfhbd

06/27/2022, 7:53 AM
Yeah, I thought the same. What about specifying
toString(): Nothing
too?
g

Grégory Lureau

06/27/2022, 7:54 AM
Good idea!
5 Views