342 points · by fanf2 · 8y ago · 107 comments
Basically, with Rust, you can import syntax like this much like you'd import a library in other languages.
Macros aren't complex, c preprocessor macros are complex. Although generally, magic that introduces names that aren't defined anywhere is bad, whether it's c macros or a python metaclass, but the metaclass at least has some direct connection to the magic name.
This is completely disregarding C and its token macros.
At its core the C preprocessor language is a simple token substitution scheme (meaning it strictly operates on the word level). I couldn't write one without looking at the spec because it has some ugly edges. But fundamentally it's a simple technology. It's flexible but that also means it must be used carefully.
These end up existing anyways though. At least a few places are using Go with their own preprocessor - isn't Kubernetes doing this?
For example, how do you step a macro expansion in Rust at any given point in a source level debugger?
Yep, you don't.
Even on Lisp, if we want something more developer friendly than (macroexpand) the only option are the commercial Common Lisp environments.
Having said this, I also do like having macro support around, provided it gets used judiciously.
> Yep, you don't.
I don't see any technical reason why that can't be done
But I have to ask, will the choice of the MPL as the license have any effect on its potential adoption? Yew is Apache/MIT, matching Rust and much of the ecosystem, for context.
MPL, for folks who aren't aware, is a successor to the CDDL seen in Solaris etc. and is a per-file copyleft, thus having a nicer legal structure while getting the same rough benefits of GPL or at least LGPL. The idea is “any modifications to this file must also be open-sourced under MPL, but you can package this file with proprietary other files that are not MPL and integrate them into a larger proprietary thing as long as your modifications to this file alone are open-sourced.” The goal is to protect weakly from Microsoft-esque “embrace, extend, extinguish” as GPL does but enable commercial integration the way BSD does.
In practice nobody seems to be all that pissed at Mozilla because of their license; the MPLv2 added GPL compatibility so the GPLers are mostly able to use MPL software and it gives a nod towards the stronger copyleft they like; commercial applications which just use the software as a library don't mind.
I'm very glad that this is happening, the whole project got started when I was struggling to write PHP/JS in the early 2000s and the itch was getting worse and worse.
The key idea is to traverse a tree datastructure which will map to database entities, for instance:
database my_db {
int /basic/i
float /basic/f
string /basic/s
stored /r
intmap(stored) /imap
}
Kind of an ORM but without objects (Opa is purely functional) and without relations when using NoSQL.But DbGen has support for several types of database including PostgreSQL: See for instance this blog http://www.josetteorama.com/to-sql-from-nosql/
<!-- The table element
Content: In this order:
optionally a caption
element, followed by zero
or more colgroup
elements, followed
optionally by a thead
element, followed by
either zero or more tbody
elements or one or more
tr elements, followed
optionally by a tfoot
element, optionally
intermixed with one or
more script-supporting
elements. -->
<!ELEMENT table - -
(caption?,colgroup*,
thead,
(tbody*|tr+),tfoot?)
+(%scripting;)>
Regular content types are pretty fundamental to markup languages.> The structure validation is simplistic by necessity, as it defers to the type system: a few elements will have one or more required children, and any element which accepts children will have a restriction on the type of the children, usually a broad group as defined by the HTML spec. Many elements have restrictions on children of children, or require a particular ordering of optional elements, which isn't currently validated.
It's not complete or thorough at present.
Regarding your link: I'm impressed someone took the time to write a DTD for HTML5!
Haskell has had something similar for many years now [1]. Ultimately the development cycle can become a big issue for this kind of thing (how long does it take to go from editing your type-checked template to being able to see that in the browser). In the Haskell version we did have a development version that allowed certain types of changes to show up quickly.
[1] https://www.yesodweb.com/book/shakespearean-templates#shakes...
https://ocsigen.org/eliom/1.3.4/manual/html
The syntax is fairly lightweight, just << followed by valid, type checked XHTML ended by >>. There are also antiquotations so you can use it as a templating language.
For those of us generating XML from C I wrote a hairy set of C macros:
https://github.com/libguestfs/libguestfs/blob/master/common/...
so you can write code like this (which is not fully checked at compile time of course):
https://github.com/libguestfs/libguestfs/blob/4aa712d551f9d4...
Note that tyxml goes quite further than Rust's typed-html: the nesting is significantly more flexible, type inference is still complete, and it will verify additional properties like "don't use <a> inside <a>". It can also be used conjointly with reactive and/or isomorphic programming.
[1]: https://ocsigen.org/tyxml/ [2]: https://ocsigen.org/tyxml/4.3.0/manual/ppx
- How compositional it is ? I have find that some of the HTML properties are very hard to verify in a compositional way, see https://github.com/ocsigen/tyxml/issues/175
- How do you handle the "subtyping" aspect that is intrinsic to HTML ? Or phrased in another way: what's your type encoding ? :)
- I suppose you desugar to a set of combinators, but you don't really expose those. Why ?
Why indeed. I personally find these "HTML in a programming language" tools pretty unappealing, because you have to give up all the normal tools of the programming language while using them [1].
Whereas you can usually write a really simple internal DSL to describe HTML that is as clear and concise as HTML, while being as convenient and powerful as the programming language. My quick and dirty efforts at this usually end up looking something like this:
html(
head(
title("My First Page")),
body(
h1("Welcome"),
p($("Made by "), a(href(), $("Tom"))),
userIsOldSchool()
? p("Bring back HTML 3.2!")
: div("Made with HTML 5.")))
The typed-html crate has an object model like this underneath, but doesn't, AFAICS, expose it.[1] With the honourable exception of ScalaTags, which i still wouldn't touch with a bargepole
A component example would be very useful. Being able to abstract away chunks of your views into discrete parts is a pretty important feature.
One big thing that's not mentioned in the docs is whether or not it handles escaping values for you, as well as an equivalent of React's dangerouslySetInnerHTML [0] when you wish to embed HTML.
[0] https://reactjs.org/docs/dom-elements.html#dangerouslysetinn...
[1] https://docs.rs/typed-html/0.1.0/typed_html/elements/struct.... [2] https://docs.rs/typed-html/0.1.0/typed_html/elements/struct....
let doc: DOMTree<String> = html!{
<body>
<img alt="xxx" />
<p alt="xxx"></p>
</body>
};
If you try to give <p> an alt attribute, the program fails to compile. error[E0609]: no field `alt` on type `typed_html::elements::Attrs_p`
--> src/main.rs:12:16
|
12 | <p alt="xxx"></p>
| ^^^ unknown field
|
= note: available fields are: `accesskey`, `autocapitalize`, `class`, `contenteditable`, `contextmenu` ... and 9 othersI think that the best solution in Haskell would be to group the types in a typeclass. However then the typeclass is not directly associated with the p or img value constructors. I wonder if there is some way to use dependent types to improve this situation.
<p>hello</p><img src=#>
If you try to access the `.alt` property of the <p> tag you get `undefined`, and if you try to access the `.alt` property of the <img> tag it returns the empty string in this case because the property exists on that tag, but hasn't been set to anything.http://www.typescriptlang.org/play/#src=%0D%0A%0D%0Atype%20A...
And I imagine its quite a bit more work than just the tags
Render to a virtual DOM... pass it on to your favourite virtual DOM system
Curious what'd it be like to wire this up with virtual-dom or maybe even React itself.Also curious how this compares to https://github.com/DenisKolodin/yew (which I'm not familiar with).
Edit: Sure, having automatic HTML escape for values is good. I work with JSX every day, and I can't really imagine having it all in one huge chunk and no code reuse. Also, what about conditional hide/show and lists?
Edit: You could probably create a separate function with an `html!` return and call that from the first? If the return types line up, which they should, I don't see why you couldn't do that.
<table>
<caption>Example Table
<thead>
<tr>
<th>Col 1
<th>Col 2
<th>Col 3
<tbody>
<tr>
<td>One
<td>Two
<td>Three
<tfoot>
<tr>
<td>Four
<td>Five
<td>Six
</table>
I'm currently drafting an article of HTML you never need to write, currently I'm covering:- some start tags are implied and never need to be written unless you're adding attributes
- some end tags are implied and never need to be written
- default attributes never need to be specified
- element values often don't require quoting
- no trailing slashes are required on void tags