Joining two Texts

I’m not sure if I’m even doing this right, but for some reason this doesn’t work:

Text text1 = Texts.of("hi");
text.builder().append(Texts.of(", you!");

text1 stays “hi”

I also can’t use Texts.join(text1, Texts.of(", you!")); because there are 2 similar overloads for this special case:

Texts.join(Text seperator, Text...texts)
Texts.join(Text...texts)

This works:
text1 = Texts.of(text1.builder.append(Texts.of(", you!")));

But shouldn’t the above also work?

Text is immutable, just like String is in Java, so in your first sample the object represented in text1 will always be the same, you can’t mutate it So, you build a new text object.

Text text1 = Texts.of("hi");
text1 = text1.builder().append(Texts.of(", you!")).build(); // <-- A new text object

To fix Texts.join, do

Texts.join(new Text[] { text1, Texts.of(", you!") });

to remove the ambiguity for the method signature

1 Like