WingedSeal / jmc

A compiler for JMC (JavaScript-like Minecraft Function), a mcfunction extension language for making Minecraft Datapack
https://jmc.wingedseal.com
MIT License
64 stars 8 forks source link

[FEATURE_REQUEST] Null coalescing assignment #17

Closed Nico314159 closed 1 year ago

Nico314159 commented 1 year ago

Description

In JavaScript, the x ??= y operator will assign x the value of y only if x is null. This is highly useful for setting default values of variables. To Minecraft-ify this, I propose that the syntax be:

$<variable> ??= <integer>;
$<variable> ??= $<variable>;

which compiles down to

execute unless score <foo> <objective> = <foo> <objective> run scoreboard players set <foo> <objective> <integer>;
execute unless score <foo> <objective> = <foo> <objective> run scoreboard players set <foo> <objective> = <bar> <objective>;

This would be useful for the same reason as in the original JavaScript, as initializing scoreboard variables is a common need.

Nico314159 commented 1 year ago

Already set up a GitHub branch for this one too: https://github.com/Nico314159/jmc/tree/null-coalescing