Hi,
Historically, we've avoided enums in Skylark.
For example, the cfg attribute is a string, e.g. cfg="host".
The reasoning was that Skylark is a dynamic language. Whether we use a symbol or a string, we can only detect typos at runtime. We also wanted to limit the number of concepts and types.
But having an enum is still useful for tooling. Linters can detect typos statically, refactoring tools can do a much better job, IDEs can provide better completion. We can also have cross-references (e.g. to find users of a specific value, or to find the list of possible values).
It turns out, we don't really need a new type or a new function for that.
We can use providers:
_colorInfo = provider(fields = ["name"])
colors = struct(
red = _colorInfo(name="red"),
green = _colorInfo(name="green"),
blue = _colorInfo(name="blue"),
)
Example of use:
load("//:colors.bzl", "colors")
_color_to_hex = {
colors.red: "#ff0000",
colors.green: "#00ff00",
colors.blue: "#0000ff",
colors.white: "#ffffff",
}
def print_name_and_hex(color):
print_name_and_hex(colors.red)
Is it a pattern we should recommend?
--
Laurent