I am playing with the pointfreeco/swift-html HTML DSL. The core idea of the library is to offer a number of functions to build an HTML node tree using an ergonomic syntax:
let node = div([class("container")], [
h1([
a([href("/")], ["Home"])
]),
])
This works very well (if you’re in the market for HTML generation, give it a go!), but I keep running into name clashes. One simple case is the class
name that’s used both in HTML and in Swift. So this doesn’t compile:
// <small class="sample">whatever</small>
// error: expected expression in list of expressions
// error: consecutive statements on a line must be separated by ';'
// error: expected identifier in class declaration
let foo = small(class("sample"), "whatever")
The class
function needs to be disambiguated using backticks or a namespace prefix:
let foo = small(`class`("sample"), "whatever")
let foo = small(Html.class("sample"), "whatever")
But this adds unwanted noise into the DSL. Is there a better solution? Could the compiler be “smarter” about expressions such as class("sample")
and not require the backticks, recognizing the function call automatically? Or, in the very least case, bail with a better error message? I realize this may have some significant downsides, but I figured it doesn’t hurt to ask at least.
PS. I am aware of the simple let klass = Html.class
sort of workaround, but I am kind of interested in a more general solution.