Skip to content
Phil Hagelberg edited this page Apr 23, 2023 · 2 revisions

Require Field Notation

This is a design that attempts to solve similar problems as parser macros but with a more limited scope.

Rationale

While metatables allow you to customize the behavior of tables at runtime, there is currently no way for such tables to have literal notation which would allow them to be serialized and round-tripped back thru the parser. For instance, if you implement a data structure like a set using a metatable on a regular table, currently fennel.view will turn it a regular table.

Solution

The metatable in question should emit the table in a notation like this:

(@require:my.lib/set [:a :b :c])

This parses to the following form:

((. (require :my.lib) :set) [:a :b :c])

It causes the module my.lib to be required. The field set is then looked up inside that module, and the table [:a :b :c] is passed to it. The intent is that the set field would take this table and return a table with the metatable constructed in such a way as to make it behave like a set.

At Macros

This is built on a more general-purpose "at-macro" system which we could possibly use for other things in the future, but for now we're more focused on the require-field notation as an application of at-macros to explore their usefulness.

Clone this wiki locally