Could you share a concrete example were an AST would be useful for debugging?
Sure. If we take a closer look at the example:
contract.requires(x != null, "Developer has to type some explanatory string...");
contract.requires(quote(x != null));
we can consider that requires()
, in the second example, receives an Expression
or a Node
object rather than a Boolean
and a string
. Accordingly, it could process the AST to detect that the requirement is or involves a nullity check. If x
were null
, then an Error
object could provide information about x
being null
. So, again in theory, an Error
object could stringify the AST or otherwise process it to automatically provide developers with more information. This would, apparently, require evaluate()
on Expression
or Node
to obtain a resultant Boolean
...
One can already use
Function.prototype.toString
to get the sourcecode of a function object and then parse that with a parser of your choice.
Function.prototype.toString()
, as implemented by Node.js (V8), has not met all of my needs. For example, as I remember, when a function contains nested anonymous functions or arrow functions, the provided text representation of the function elides or only refers to its nested functions.
Here again I wonder what concrete usecases you'd have in mind, which can't already be solved through other means.
I didn't mean to make a claim about any of the above use cases not being solveable through other means or having other, potentially inelegant, workarounds. To the above example, for instance, one could reply, "but the developer could just type the error description string."
Some of the use cases with iterables and observables can be considered in response to your question. One use case that comes to mind resembles "LINQ to SQL" where AST's could be processed, potentially optimized, translated into a DSL, e.g., SQL, and transmitted to a server.
With the aforementioned iterables and observables, one can envision subscribing to streams transmitted from server machines. In this case, having the AST's of functions used to filter()
such streams would be useful for optimization scenarios, e.g., to transmit the semantics of the filtering functions to the server, reducing transmission cost. So having access to the AST's involved in formulating queries to servers can facilitate a number of optimizations.
An interesting software to consider, on these topics, is RethinkDB (source) which provides a cool JS API which includes support for something called changefeeds. With databases like RethinkDB, developers can subscribe to receive notifications when tables, rows, or, more interestingly, query results change or update.