Concatenate them in the compiler. Runtime cost: zero commands.
"hello " + "world"
should emit the single literal "hello world" wherever it is consumed.
Do not concatenate strings. Preserve a Text tree and emit multiple components.
This is normally the right implementation for chat, titles, names, and other text
component consumers.
Conceptually:
Text::concat([Text::literal("Kills: "), Text::score(player, kills)])
The backend emits one structured text component rather than constructing a new NBT string. Runtime string allocation and macro parsing are avoided.
Use a minimal macro function:
# data/mdl/function/internal/string/concat.mcfunction
$data modify storage mdl:runtime concat.out set value "$(left)$(right)"With arguments already in one compound:
{left:"hello ",right:"world",out:""}
invoke:
function mdl:internal/string/concat with storage mdl:runtime concatStatus: the macro substitution mechanism is Documented. The example is a Candidate until tested on 26.2 with escaping-heavy strings.
If all pieces are already macro arguments, one template can concatenate all of them:
$data modify storage mdl:runtime concat.out set value "$(a)$(b)$(c)$(d)"The compiler should flatten nested source concatenations before lowering. It should
not generate three runtime concatenations for (a + b) + (c + d) when one macro
instantiation can materialize the final string.
String concatenation should remain a high-level operation until its consumer is known:
Concat(String pieces)
can become:
- constant folding;
- a composite
Textvalue; - one macro-created NBT string;
- a command template with the pieces inserted directly;
- no operation at all after inlining into a surrounding macro.
Materializing an intermediate string too early prevents all of these optimizations.
"$(left)$(right)" is only correct if Minecraft's macro encoding and the surrounding
SNBT quotes safely represent every allowed source string. The compiler needs a
version-specific encoder and adversarial tests. Until those pass, this operation
must not claim to support arbitrary strings.
Potential internal distinctions include:
PlainString
SnbtStringContent
CommandToken
ResourceId
Text
They must not be freely interchangeable.
- Fold constant pieces.
- Flatten nested concatenations.
- Drop empty pieces.
- Fuse concatenation into the consuming macro when safe.
- Preserve structured text instead of flattening it.
- Reuse a materialized result only if reuse pays for storage and invalidation.
- one macro concatenation versus multiple materializations;
- cache hit versus cache miss for repeated strings;
- direct composite text versus prebuilt NBT string;
- short versus large strings;
- quoting and Unicode correctness.