Acceptable Named Tuple Names?

I’m working on functions to create html tags and am hoping to create named tuples with names that might include a hyphen for certain attributes. Can named tuples have names with a space or hyphen? How do you declare the names in that case? I have a version using Dict that works but named tuples appear to be faster and are easier to type.

Hoping for something like this:

attrs = (class = "1", style = "test", required = 1, "custom-attr" = "ok")

This seems to work:

attrs = (class = "1", style = "test", required = 1, var"custom-attr" = "ok")

This uses the @var_str string macro to create a Symbol that would otherwise not be valid.

I just learned that @var_str doesn’t really exist. The var"" syntax looks like a string macro, but it is actually implemented directly by the parser, according to the doc string.


Thanks. This might be a different question, but can you make a named tuple with only one item? Or is there a different data type that I’d have to use to handle this case?

attrs = (class = "1")

Just like with a normal Tuple, you need a trailing comma in there when you only have a single element:

attrs = (class = "1",)

You can also use an initial semi-colon to mark it as a NamedTuple:

julia> attrs = (; class = "1")
(class = "1",)