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.
Update:
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.
4 Likes
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?
ex:
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",)
2 Likes
You can also use an initial semi-colon to mark it as a NamedTuple
:
julia> attrs = (; class = "1")
(class = "1",)
4 Likes