Imagine this...
async function main () {
const config = await (async (
a = 'a',
b = 'b',
c = `${a}/${b}`,
d = ((
e = 'e',
f = ((
g = a,
) => ({
g,
}))(),
) => ({
e,
f,
}))(),
h = import('./h'),
i = d.e,
) => ({
a,
b,
c,
d,
h: await h,
i,
}))();
console.log(config);
}
main();
This is actually totally possible, and you can run it above in ts-node
.
However it's a bit verbose, so maybe if there was some macros to help.
Note that there's no mutual recursion here, and arrow functions are acting as the binders.
Another thing is that it's not really lazy. The promises are asynchronous, but not lazily evaluated.
Would be awesome to have a lazily evaluated DSL for configuration in JS.
Most important thing is that it's completely type-safe, and the lack of mutual recursion means that it's not possible to have infinite loops.
Mutual recursion could still be useful though in some cases.
See: