Comment by kragen
6 years ago
This is beautiful. I never thought of chaining the parenthesis-free syntax or using table arguments for a sequence like that.
6 years ago
This is beautiful. I never thought of chaining the parenthesis-free syntax or using table arguments for a sequence like that.
You can accomplish something similar in C++: single argument constructors let you skip the parentheses but that single argument can be an initializer_list, for example 'Tag(std::initializer_list<Tag> children)'. Now create subclasses of Tag for actual tags and you end up with a DSL that looks just like the one in the article. There is of course a bit more to it, but it can be done.
Skip the parentheses? Do you mean like
Or is this an aspect of C++ I don't know about?
Yes exactly. And now if the constructor of your element, let's say a div, doesn't take a string but a std::initializer_list you can write it like this:
1 reply →
How would you do attributes?
You could use the builder pattern:
Div { H1("Title") }.Attr("class", "footer")
1 reply →