4 ms·
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
by pb82 7y ago
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.
- patrec 7y agoHow would you do attributes?
- pb82 7y agoYou could use the builder pattern: Div { H1("Title") }.Attr("class", "footer")
- patrec 7y agoThen you dont really end up with a DSL that looks just like the one in the article, though – this is noticeably less nice :) The best I could think off were designated initializers like: Div{{.class = "footer"}, B{"some text"}} But making tags, attributes and plain text work nearly as nicely as in the lua table example seems not super straightforward to me.
- kragen 7y agoSkip the parentheses? Do you mean like H1 x = "Topics"; Or is this an aspect of C++ I don't know about?