Move towards a return value based style for our code generating methods #3546
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The methods in Dafny's code generating back-ends often use side-effects to emit generated code.
And example is the method
TrExpr
:Instead of returning a value, it writes its output to
ConcreteSyntaxTree wr
.The goal of this PR is not to make a particular code change, but to hopefully convince the reader that we should stop promoting the above side-effect style, and instead promote a more pure style, in which
TrExpr
looks like:There's two advantages to the more pure style, one is readability and the other is reusability.
Readability
This side-effect style sometimes leads to hard to read code, such as:
The pure style is much shorter and easier to read:
Commit 01b1416 and 0e7e0b8 show examples of how the pure style can look nicer than the side-effect one.
Reusability
The side-effect style of compilation is problematic when trying to generalise the code so it can be used to create an AST.
To construct an AST we want to be able to write code such as:
Note that we're using an
Expr
method that returns a value. If instead we had used a methodTrExpr
that writes its results to some store, we would have needed code like the following to produce an AST, which is hard to read and write:By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.